Ios 重用UITableViewCell';没有重复数据的数据
我在多个自定义Ios 重用UITableViewCell';没有重复数据的数据,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我在多个自定义UITableViewCell重用方面遇到一些问题。我有3种类型的自定义UITableViewCell。我尝试使用dequeueReusableCellWithIdentifier重用UITableViewCell 我编写了prepareforeuse,并使所有可能的子视图数据为零。但我还是在一些UITableViewCell中得到了一些重复的数据。所以我想在没有任何数据的情况下重用xib 我编写了以下代码。该应用程序现在似乎有一些冻结问题。有人请告诉我在不重复数据的情况下重用ta
UITableViewCell
重用方面遇到一些问题。我有3种类型的自定义UITableViewCell
。我尝试使用dequeueReusableCellWithIdentifier
重用UITableViewCell
我编写了prepareforeuse
,并使所有可能的子视图数据为零。但我还是在一些UITableViewCell
中得到了一些重复的数据。所以我想在没有任何数据的情况下重用xib
我编写了以下代码。该应用程序现在似乎有一些冻结问题。有人请告诉我在不重复数据的情况下重用tableviewUITableViewCells
的正确方法
static NSString *cellIdentifier = @"SystemListCell";
IXSystemMessageCustomCell *cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"IXSystemMessageCustomCell" owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
IXSystemMessageCustomCell *cellToSave = cell;
[self configureSystemCell: cellToSave atIndexPath:indexPath];
return cellToSave;
请勿修改
prepareforeuse
更好的方法是
dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath
我们应该始终修改cellforrowatinexpath
中的数据,因为这有助于单元格的重用
另外,由于您已经创建了IXSystemMessageCustomCell
类,因此最好使用以下方法:
[[IXSystemMessageCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
并在initWithStyle:reuseIdentifier:
与此相反:
cell = [[[NSBundle mainBundle] loadNibNamed:@"IXSystemMessageCustomCell" owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
干杯 您可以对3种单元格类型使用3种不同的重用标识符:
IXSystemMessageCustomCell *cell;
if (some condintion) {
cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:"CellReuseIdentifierType1"];
} else if (some condition 2) {
cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:"CellReuseIdentifierType2"];
} else {
cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:"CellReuseIdentifierType3"];
}
在这种情况下,您不需要每次为每种单元格类型重新配置单元格
IXSystemMessageCustomCell *cell = nil;
cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
通过首先将单元格设置为nil,可以清除与重用单元格关联的先前数据
还可以在viewDidLoad
或类似的表视图生命周期方法中注册nib一次,这样就不需要在每次调用tableView:cellforrowatinexpath
时都注册nib
所以从任何方法中提取静态调用作为私有声明
static NSString *cellIdentifier = @"SystemListCell";
并调整你的阅读方法
- (void)viewDidLoad {
UINib *nib = [UINib nibWithNibName:@"IXSystemMessageCustomCell" bundle:nil];
[<<self.tableView>> registerNib:nib
forCellReuseIdentifier:cellIdentifier];
<< ... other code ... >>
}
您的意思是有3种类型的单元格,但它们的显示方式与您的需要不同?还是只有单元格中的数据在重复?单元格中的数据在重复。请注意,我正在使用xibs创建自定义单元格。如果有3个不同的单元格布局,则必须使用不同的标识符。仅供参考,您不再需要检查单元格==nil,尽管很多文档仍然提到了这一点。您可以添加用于将内容添加到单元格中的代码吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
IXSystemMessageCustomCell *cell = nil;
cell = (IXSystemMessageCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureSystemCell:cell atIndexPath:indexPath];
return cell;
}