UITableView
에 데이터를 표시하는 데 문제가 있습니다. 나는 그것이 세포 재사용과 관련이 있다고 믿습니다. 나는 온라인에서 여기를 확인했으나 나에게 적합한 해결책을 찾지 못했습니다. 어떤 도움을 주시면 감사하겠습니다.대기열에서 대기열을 비우기위한 테이블보기 문제
텍스트 및 그림으로 채워진 배열이 있습니다. 그런 다음 tableView
에 정보를 표시하고 있습니다. 정적 크기의 셀을 사용한다면 모든 것이 정상적으로 작동하지만 텍스트의 양이 변하기 때문에 heightForRowAtIndexPath
메소드를 구현했습니다. 내가 맨 아래까지 스크롤 할 때까지 이것은 잘 작동합니다.
그런 다음, 위로 스크롤하면 모든 셀 높이가 변경되고 디스플레이가 모든 뒤죽박죽이됩니다. 일부 텍스트가 잘 리거나 그림이 잘리고 일부 셀에만 텍스트의 마지막 부분 만 있습니다. 정말 세포 재사용과 관련이 있다고 생각하지만이 문제를 어떻게 공격해야할지 모르겠습니다. 아래 코드는 cellForRowAtIndexPath
과 heightForRowAtIndexPath
입니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
NSString *label = [_theBigArray objectAtIndex:indexPath.row];
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)];
textV.font = [UIFont systemFontOfSize:15];
textV.text = [_theBigArray objectAtIndex:indexPath.row];
textV.textColor = [UIColor blackColor];
textV.editable = NO;
[cell.contentView addSubview:textV];
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)];
imageV.contentMode = UIViewContentModeScaleAspectFit;
imageV.image = [_theBigArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:imageV];
}
return cell;
[tableView reloadData];
}
는 heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight = 0.0f;
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
NSString *temp = [_theBigArray objectAtIndex:indexPath.row];
CGSize size = [temp sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
rowHeight = size.height+50;
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
rowHeight = 115.0f;
}
//NSLog(@"rowHeight is %i", rowHeight);
return rowHeight;
[tableView reloadData];
}
들어 난 두 개의 서로 다른 세포를 만들어 별도로 전화를 시도했지만 같은 일이 발생합니다. 나는 여전히 동일한 heightForRowAtIndexPath
방법을 사용했습니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *newCell = [[UITableViewCell alloc] init];
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSString *label = [_theBigArray objectAtIndex:indexPath.row];
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)];
textV.font = [UIFont systemFontOfSize:15];
textV.text = [_theBigArray objectAtIndex:indexPath.row];
textV.textColor = [UIColor blackColor];
textV.editable = NO;
[cell.contentView addSubview:textV];
newCell = cell;
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PictureCell"];
}
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)];
imageV.contentMode = UIViewContentModeScaleAspectFit;
imageV.image = [_theBigArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:imageV];
newCell = cell;
}
return newCell;
[tableView reloadData];
}
아이디어가 있으십니까?
빠른 답변 감사합니다! 나는이 것들을 밖으로 시도하고 다시 당신에게 돌아갈 것입니다. 시간 내 주셔서 감사합니다. – Douglas
감사합니다. 도움이된다면 답변을 승인으로 표시하십시오. –
그래서 나는 당신이 연결 한 그 게시물을 보았습니다. 그러나 그들은 cellForRowAtIndexPath 메서드 외부에 서브 뷰를 만들고 태그를 붙인 다음 메서드에 추가합니다. 그러나 나는 그 방법으로 그들을 창조했다. 내가 그들을 어디에서 만들어야합니까? 이후 배열 요소에 대한 정보를 만들어야합니다. 나는 또한 당신의 충고에 따라 몇 가지를 바 꾸었습니다. 고마워. – Douglas