如何在tableview的行中创建多个列?
最佳答案
UITableView 并不是真正为多列设计的。但是您可以通过创建自定义 UITableCell 类来模拟列。在 Interface Builder 中构建自定义单元格,为每一列添加元素。给每个元素一个标签,这样你就可以在你的 Controller 中引用它。
给你的 Controller 一个 socket 来从你的 Nib 加载电池:
@property(nonatomic,retain)IBOutlet UITableViewCell *myCell;
然后,在您的 TableView 委托(delegate)的 cellForRowAtIndexPath
方法中,通过标记分配这些值。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// load cell from nib to controller's IBOutlet
[[NSBundle mainBundle] loadNibNamed:@"MyTableCellView" owner:self options:nil];
// assign IBOutlet to cell
cell = myCell;
self.myCell = nil;
}
id modelObject = [myModel objectAtIndex:[indexPath.row]];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [modelObject firstField];
label = (UILabel *)[cell viewWithTag:2];
label.text = [modelObject secondField];
label = (UILabel *)[cell viewWithTag:3];
label.text = [modelObject thirdField];
return cell;
}
关于ios - iOS Tableview 中的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3252682/