xcode 索引路径行的表视图方法单元再次重新加载然后应用程序崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6465367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Table view method cell for row at index path again reloading and then app crashing
提问by Christina
In my app I have a table view and 4 images view in one row. When I scroll the table view then cell for row at index path method is again called and then the app crashes. How can I prevent table view reloading when scrolling. My part of code is :-
在我的应用程序中,我在一行中有一个表格视图和 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;
}
Please help. Any help will be appreciated.
请帮忙。任何帮助将不胜感激。
Thanks, Christy
谢谢,克里斯蒂
回答by Deepak Danduprolu
The only problem I see is this,
我看到的唯一问题是这个,
for ( int i = 1; i <= photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self showImage:imageView];
}
What is j
here? I suggest you change that to i
i.e.
什么是j
在这里吗?我建议你把它改成i
ie
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self showImage:imageView];
}
As such only other flaw I see is the logic here,
因此,我看到的唯一其他缺陷是这里的逻辑,
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];
}
You only need to add 4 image views in a row. Not the the total number of image views in each row. This is flawed logic. Suggested update to
您只需要连续添加 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];
}