通常使用reloaddata函数重新加载表中的数据,但如果我想更改为不同类型的UITableViewCell,该怎么办?用新的UITableViewCells重新加载UITableView
基本上我喜欢动态调用
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
它允许不同类型的细胞,以进行加载。
通常使用reloaddata函数重新加载表中的数据,但如果我想更改为不同类型的UITableViewCell,该怎么办?用新的UITableViewCells重新加载UITableView
基本上我喜欢动态调用
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
它允许不同类型的细胞,以进行加载。
你尝试过设置一个NSString
属性,然后使用该作为dequeueReusableCellWithIdentifier:
参数。那么打电话给reloadData
可能会换出细胞?
自己没有尝试过 - 只是一个想法。
尝试
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
,并传入要重新加载索引的数组。它会为你调用cellForRowAtIndexPath回调。
定义一个typedef为不同的细胞类型:
typedef enum {
kTableCellType1,
kTableCellType2
} TableCellType;
然后,定义使用新TableCellType,例如一个类的实例变量
@interface MyTableViewController()
{
TableCellType _tableCellType;
}
@end
在viewDidLoad
,初始化这个实例变量:
- (void)viewDidLoad
{
[super viewDidLoad];
// do all of your other initialization
_tableCellType = kTableCellType1;
}
然后,你cellForRowAtIndexPath
可以查看使用什么类型的细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_tableCellType == kTableCellType1)
{
// build first table cell type
}
else if (_tableCellType == kTableCellType2)
{
// build the other table cell type
}
}
最后,当你想改变你的细胞,你改变你的伊娃,然后重新加载数据:
_tableCellType = kTableCellType2;
[self.tableView reloadData]; // or reloadRowsAtIndexPaths or reloadSections