通常、テーブルにデータをリロードする関数 reloaddata を使用しますが、別のタイプの UITableViewCell に変更したい場合はどうすればよいですか?
基本的に私は動的に呼び出すのが好きです
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
これにより、さまざまなタイプの細胞をロードできます。
通常、テーブルにデータをリロードする関数 reloaddata を使用しますが、別のタイプの UITableViewCell に変更したい場合はどうすればよいですか?
基本的に私は動的に呼び出すのが好きです
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
これにより、さまざまなタイプの細胞をロードできます。
NSString
プロパティを設定して、それをdequeueReusableCellWithIdentifier:
パラメータとして使用しようとしましたか。次にreloadData
、セルをスワップアウトする可能性がありますか?
これを自分で試したわけではありません-ただの考えです。
さまざまなセル タイプの 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
試す
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
リロードするインデックスの配列を渡します。cellForRowAtIndexPath コールバックが呼び出されます。