在我的应用程序中,我有一个表视图和一行中的4个图像视图。当我滚动表视图时,再次调用索引路径方法的行的单元格,然后应用程序崩溃。如何在滚动时阻止表视图重新加载。我的代码部分是: -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
for (int i=0; i <= [wordsInSentence count]; ++i) {
UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
imageView1.tag = i+1;
[imageViewArray insertObject:imageView1 atIndex:i];
[cell.contentView addSubview:imageView1];
}
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [wordsInSentence count] % 4;
}
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self showImage:imageView];
}
return cell;
}
请帮忙。任何帮助将不胜感激。
谢谢, 克里斯蒂
答案 0 :(得分:1)
我看到的唯一问题就是这个,
for ( int i = 1; i <= photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self showImage:imageView];
}
这里j
是什么?我建议你改为i
,即
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self showImage:imageView];
}
因此,我看到的其他缺陷就是这里的逻辑,
for (int i=0; i <= [wordsInSentence count]; ++i) {
UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
imageView1.tag = i+1;
[imageViewArray insertObject:imageView1 atIndex:i];
[cell.contentView addSubview:imageView1];
}
您只需要连续添加4个图像视图。不是每行中图像视图的总数。这是有缺陷的逻辑。建议更新
for (int i = 0; i < 4; ++i) {
UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
imageView1.tag = i+1;
int trueImageIndex = indexPath.row * 4 + i;
[imageViewArray insertObject:imageView1 atIndex:trueImageIndex];
[cell.contentView addSubview:imageView1];
}