【问题标题】:iPhone :UITableView CellAccessory CheckmarkiPhone :UITableViewCell 配件复选标记
【发布时间】:2011-08-23 00:42:33
【问题描述】:

在 iPhone 应用程序中单击表格视图单元格我想显示表格视图单元格附件类型复选标记为 didSelectRowAtIndexPath 我正在编写代码

if(indexPath.row ==0)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
 }

并显示复选标记。

但在我的情况下,我不允许用户一次只检查单元格,这意味着如果用户选择其他行,则应该检查该行,并且应该取消选中之前检查过的行

我怎样才能做到这一点?

【问题讨论】:

标签: iphone objective-c uitableview ios4


【解决方案1】:

在实例变量中跟踪检查了哪一行。 当用户选择新行时,首先取消选中之前选中的行, 然后检查新行并更新实例变量。

这里有更多细节。首先添加一个属性来跟踪当前选中的行。如果这是NSIndexPath,这是最简单的。

@interface RootViewController : UITableViewController {
    ...
    NSIndexPath* checkedIndexPath;
    ...
}

...
@property (nonatomic, retain) NSIndexPath* checkedIndexPath;
...

@end

在您的cellForRowAtIndexPath 中添加以下内容:

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else 
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

您如何编码tableView:didSelectRowAtIndexPath: 将取决于您想要的行为。 如果必须始终检查一行,也就是说,如果用户单击已检查的行,请使用以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

如果您想让用户能够通过再次单击该行来取消选中该行,请使用以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                    cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    if([self.checkedIndexPath isEqual:indexPath])
    {
        self.checkedIndexPath = nil;
    }
    else
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
    }
}

【讨论】:

  • 不错。深思熟虑。彻底。大量使用代码示例。干得好,感谢您的巨大贡献!
  • 你应该在你的didSelectRowAtIndexPath 中添加[tableView deselectRowAtIndexPath:indexPath animated:YES];
  • @KaiBurghardt 不,我不应该 UITableView 会为你这样做。
  • 如果 uncheckCell 不在 tableView 的可见单元格中,则会崩溃。
  • @umakanta 你能发布更多信息吗?它在哪里崩溃?为什么?
【解决方案2】:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
    int newRow = [indexPath row];
    int oldRow = [lastIndexPath row];
    if (newRow != oldRow)
    {
        newCell = [tableView  cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath = indexPath;  
    }
}

参考看看这个discussion

【讨论】:

    【解决方案3】:

    这是一个对我有用的简单解决方案:

    在您的 .h 文件中声明:

    NSIndexPath *checkedCell;
    

    然后在您的 .m 文件中添加:

            if (checkedCell == indexPath) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
            } else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    }
    

    cellForRowAtIndexPath 方法和

    checkedCell = indexPath;
    [tableView reloadData]
    

    didSelectRowAtIndexPath 方法。

    祝你好运!

    【讨论】:

      【解决方案4】:

      我解决了您的问题并找到了解决方案

      在.h文件中

      int rowNO;
          NSIndexPath *lastIndexPth;
      

      在 . m文件

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          NSLog(@"%d",indexPath.row);
          if (rowNO!=indexPath.row) {
              rowNO=indexPath.row;        
              [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
              [tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
              lastIndexPth=indexPath;
          }
      

      希望对你有所帮助...

      【讨论】:

        【解决方案5】:

        很简单

        在.h中

        NSIndexPath   checkedCell;
        

        在.m

        cellForRowAtIndexPath
        
        
        if ([checkedCell isEqual:indexPath]) 
        
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        
        } else
        { 
            cell.accessoryType = UITableViewCellAccessoryNone; 
        }
        

        在 didSelectRowAtIndexPath

        checkedCell = indexPath;
        
        [tableView reloadData]
        

        【讨论】:

          【解决方案6】:

          您可以将选中行的NSIndexPath 存储为类变量,每当您尝试访问其他行时,只需使用存储为类变量的NSIndexPath 变量访问先前选中的行并取消选中。

          查看下面的帖子

          http://www.iphonedevsdk.com/forum/iphone-sdk-development/24240-uitableviewcellaccessorycheckmark-one-checkmark-time.html

          【讨论】:

            【解决方案7】:

            添加到您的UITableViewDataSource 委托变量NSIndexPath *checkedIndex。添加到didSelectRowAtIndexPath:

            checkedIndex = indexPath;
            [tableView reload];
            

            并更改您的 cellForRowAtIndexPath: 方法:

            cell = .. // init or dequeue
            if ([indexPath isEqualTo:checkedIndex])
            {
                cell.accessoryType=UITableViewCellAccessoryCheckmark;
            }
            else
            {
                cell.accessoryType=UITableViewCellAccessoryNone;
            }
            

            【讨论】:

              【解决方案8】:

              希望这篇文章能解决你的问题:

              - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
              
                  int newRow = [indexPath row];
                  int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
                  
                  if (newRow != oldRow)
                  {
                      UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                                  indexPath];
                      newCell.accessoryType = UITableViewCellAccessoryCheckmark;
                      
                      UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: 
                                                  lastIndexPath]; 
                      oldCell.accessoryType = UITableViewCellAccessoryNone;
                      lastIndexPath = indexPath;
                  }
                  
                  [tableView deselectRowAtIndexPath:indexPath animated:YES];
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2010-12-06
                • 1970-01-01
                • 2014-10-17
                • 2015-06-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多