在UITableviewcell中存储布尔值

时间:2012-02-20 11:51:52

标签: iphone ios uitableview boolean

如何在UITableview的每一行中存储一个布尔值。当需要选择特定单元格时,我需要检索存储在单元格中的布尔值。

5 个答案:

答案 0 :(得分:2)

有很多方法:
 1.您可以使用UITableViewCell.tag属性
 2.您可以创建自己的继承自UITableViewCell的单元类,并为您添加正常属性BOOL值
 3.您可以使用与tableview关联的数组,当您选择单元格时,只需使用indexPath查找数组中的关联值 等

答案 1 :(得分:1)

我建议在UITableViewCell中使用tag属性。

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;            
// returns nil if cell is not visible or index path is out of range
{

    static NSString *identifier = @"MyIndetifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
  }
  //make sure tu put here, or before return cell. 
  cell.tag = 0; //0 =NO, 1=YES;

  return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    BOOL boolean = cell.tag; // return 0 or 1. based on what boolean you set on this particular row.
}

答案 2 :(得分:0)

您可能还有其他存储空间,可以保存表格单元格标题或副标题。在那里存储布尔值。使用它将bool转换为可以放入数组或字典中的内容。

[NSNumber numberWithBool:YES]

例如,如果您使用NSArray字符串来存储标题,请使用字典数组。每个字典都有一个“标题”和(例如)“isActive”布尔值(存储为NSNumber)。

答案 3 :(得分:0)

NSMutableArray * tableData;

每个表格单元格与tableData中的NSMutableDictionary存储关联, 你可以将NSNumber(存储bool)设置为Dictionary。

答案 4 :(得分:0)

您需要实现UITableView的方法

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;            
// returns nil if cell is not visible or index path is out of range
{
 //create cell here
static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
  }
  return cell;
}
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   //通过使用indexpath.row,您可以访问用户单击的单元格。 }
相关问题