索引路径处的行的表视图方法单元格再次重新加载,然后应用程序崩溃
在我的应用程序中,我在一行中有一个表格视图和 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;
}
请帮忙。任何帮助将不胜感激。
谢谢, 克里斯蒂
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 :-
- (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的唯一问题是,
这里的
j
是什么?我建议您将其更改为i
即,因为我看到的唯一其他缺陷就是这里的逻辑,
您只需要连续添加 4 个图像视图。不是每行中图像视图的总数。这是有缺陷的逻辑。建议更新为
The only problem I see is this,
What is
j
here? I suggest you change that toi
i.e.As such only other flaw I see is the logic here,
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