若要在 UITableView 中使用自訂樣式,常見做法是建立自訂 UITableViewCell,並搭配 XIB 設定版面。這樣可以讓每一列的元件與排版更靈活。



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"mycell";
SettingViewCell *cell = (SettingViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SettingViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
NSString *text = _listArray[indexPath.row];
[[cell settingName] setText:text];
return cell;
}使用自訂 Cell 時,記得設定 reuseIdentifier,並在資料源方法中回傳 cell,才能正常顯示與重用。