2011-08-31 169 views
0

您需要创建一个表格视图,只有一个单元格可选择像这样img。如何创建只有一行可选的表格视图

enter image description here

+0

你做了什么来实现这一目标? – Krishnabhadra

+0

这是日历应用程序的屏幕截图 – Dany

+0

您是否知道如何创建常规表格视图?你卡在哪里? – 2011-08-31 07:16:12

回答

1
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.font = [UIFont systemFontOfSize:14]; 
    if(indexPath.row == 0) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

cell.textLabel.text = @"Sound Effects"; 
return cell; 

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 

    if(path.row == 0) 
    { 
     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
    } 
} 
+0

此代码将允许您仅选择第一行。 – Nekto

+0

这应该只适用于第一行..如果你想要它应该适用于所有的行然后删除if(path.row == 0)两种方法的条件.. – PJR

+0

太棒了!谢谢 ;) – Dany

0

,如果你只喜欢一个行选择你在表的方法来管理这样的:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 需要实现的选择逻辑。如果您单击未选定的对象,则选择它并取消选择所有其他对象,或者仅选择当前选定的对象(如果选择了一个对象)。如果单击选定的对象,只需取消在这里
  2. 不要忘记做[theTableView reloadData]; 的情况下,您的屏幕不更新,如果你改变表格的内容什么
  3. 然后重绘你只是确保你的方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 保证以某种方式标记选择的对象。这样你也可以做很棒的事情,比如显示一个切换开关按钮或任何你可能喜欢的东西。
相关问题