0

通常使用 reloaddata 函数,它只是重新加载表中的数据,但是如果我想更改为不同类型的 UITableViewCell 怎么办?

基本上我喜欢动态调用

 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

这允许加载不同类型的单元格。

4

3 回答 3

0

您是否尝试过设置NSString属性,然后将其用作dequeueReusableCellWithIdentifier:参数。然后调用reloadData可以换出单元格?

我自己没有尝试过 - 只是一个想法。

于 2012-06-29T02:04:09.407 回答
0

尝试

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

并传入要重新加载的索引数组。它将为您调用 cellForRowAtIndexPath 回调。

于 2012-06-29T02:35:57.127 回答
0

为不同的单元格类型定义 typedef:

typedef enum {
    kTableCellType1,
    kTableCellType2
} TableCellType;

然后,定义一个使用新 TableCellType 的类实例变量,例如

@interface MyTableViewController ()
{
    TableCellType _tableCellType;
}
@end

viewDidLoad中,初始化此 ivar:

- (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
    }
}

最后,当你想改变你的单元格时,你改变你的 ivar 然后重新加载数据:

_tableCellType = kTableCellType2;
[self.tableView reloadData]; // or reloadRowsAtIndexPaths or reloadSections
于 2012-06-29T03:35:00.827 回答