【发布时间】:2012-11-14 01:01:25
【问题描述】:
这就是我最终想要做的事情。我想在 UITableView 中动态显示项目菜单,以便显示的项目类型确定加载的自定义单元格视图。例如,假设菜单项类型是“switch”,那么它将加载一个名为“switch.xib”的 nib,并且状态将根据该特定菜单项的值打开/关闭。可能有 5 个项目是“开关”类型,但值不同。所以我想为每一个使用相同的 xib,但 5 个实例。
所以,这个问题还有很长的路要走。当我从笔尖加载单元格视图时,我认为它需要唯一的重用标识符用于出队,以便在屏幕上回滚时,对吗? (每个实例都是唯一的,即每个菜单项。)在 Interface Builder 的 UITableViewCell 中,我看到可以在哪里设置重用标识符属性,但我想在运行时为开关的每个实例设置它。例如,菜单项#1 是一个开关,#2 是一个文本字段,#3 是一个开关,等等。所以 #1 和 #3 都需要唯一的单元格 ID 才能出列。
这是我的 cellForRowAtIndexPath 的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Cells are unique; dequeue individual cells not generic cell formats
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];
ITMenuItem *menuItem = [menu.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load cell view from nib
NSString *cellNib = [NSString stringWithFormat:@"MenuCell_%@", menuItem.type];
[[NSBundle mainBundle] loadNibNamed:cellNib owner:self options:nil];
cell = myCell;
self.myCell = nil;
}
// Display menu item contents in cell
UILabel *cellLabel = (UILabel *) [cell viewWithTag:1];
[cellLabel setText:menuItem.name];
if ([menuItem.type isEqualToString:@"switch"]) {
UISwitch *cellSwitch = (UISwitch *) [cell viewWithTag:2];
[cellSwitch setOn:[menuItem.value isEqualToString:@"YES"]];
}
else if ([menuItem.type isEqualToString:@"text"]) {
UITextField *textField = (UITextField *) [cell viewWithTag:2];
[textField setText:menuItem.value];
}
return cell;
}
【问题讨论】:
-
您的问题是什么?你有什么问题?什么错误信息?
-
对不起,说清楚可能有点困难。问题是,如何在分配单元格时使用重用标识符标记单元格,以便出队起作用?
标签: ios objective-c uitableview nib