【问题标题】:Store boolean value in UITableviewcell在 UITableviewcell 中存储布尔值
【发布时间】:2012-03-10 18:23:55
【问题描述】:

如何在 UITableview 的每一行中存储一个布尔值。 I need to retrieve the boolean value stored in the cell, when that particular cell is selected.

【问题讨论】:

    标签: iphone ios uitableview boolean


    【解决方案1】:

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

    [NSNumber numberWithBool:YES]
    

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

    【讨论】:

      【解决方案2】:

      NSMutableArray *tableData;

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          你需要实现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 您可以访问用户单击的单元格。 }

          【讨论】:

            【解决方案5】:

            我建议在UITableViewCell 中使用标签属性。

            - (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.
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-25
              • 1970-01-01
              • 2021-12-04
              • 2019-02-10
              • 1970-01-01
              • 2015-05-08
              • 1970-01-01
              • 2012-10-15
              相关资源
              最近更新 更多