通常使用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