【问题标题】:Default CellAccessory默认细胞附件
【发布时间】:2012-10-24 19:32:49
【问题描述】:

如何设置应用程序启动时选中的第一行表格视图?我的意思是当我现在启动应用程序时,tableview 中的行数很少,当我点击其中一些时,它的附件更改为复选标记。当单击另一个时,另一个带有复选标记并且以前的复选标记消失。所以现在我希望在启动应用程序时选中 tableview 的第一行,然后当我单击另一行时,它会“取消选中标记”和“选中标记”新行等...

【问题讨论】:

    标签: objective-c tableview checkmark


    【解决方案1】:

    试试这些帖子how to apply check mark on table view in iphone using objective c?iPhone :UITableView CellAccessory Checkmark 中建议的选项

    基本上,您需要使用字典来跟踪复选标记。而在viewDidLoadinit 方法中,使第一个单元格在字典中被选中。绘制单元格时,请始终检查字典中的相应条目是否已选中,并相应显示复选标记。当用户点击一个单元格时,将字典中的值修改为选中/未选中。

    更新: 这是一个示例代码。

    在 .h 文件中声明一个属性为

    @property(nonatomic) NSInteger selectedRow;
    

    然后使用下面的代码,

    - (void)viewDidLoad {
      //some code...
    
      self.selectedRow = 0; //if nothing should be selected for the first time, then make it as -1
      //create table and other things
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // create cell and other things here
    
        if (indexPath.row == self.selectedRow)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // some other code..
    
        if (indexPath.row != self.selectedRow) {
           self.selectedRow = indexPath.row;
        }
    
        [tableView reloadData];
    }
    

    【讨论】:

    • 有很多方法可以做到这一点。一个例子在这里..joshgrenon.com/2009/07/20/… ...我已经用另一种方法更新了答案
    • 如果您使用 IB 创建它,您可以在 tableView 上使用 reloadData ViewDidLoad
    【解决方案2】:

    您可以将整数设置为类似于“checkedCell”的变量,将该值默认设置为单元格 0,并在cellForRowAtIndexPath 中检查单元格是否已被检查,如果没有则更改附件以使其被检查并更新您的变量。

    -(void)viewWillAppear{
         currentCheckedCell = 0;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //
        // Create cells -- if indexpath.row is equal to current checked cell reload the table with the correct acessories.
        //
        static NSString *cellIdentifier = @"RootMenuItemCell";
        MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) {
    
            cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            for (UIView *view in nibContents) {
                if ([view isMemberOfClass:[MyCell class]]) {
                    cell = (MyCell *)view;
                    break;
                }
            }
    
            //OTHER CELL CREATION STUFF HERE
    
            // cell accessory
            UIImage *accessory = [UIImage imageNamed:@"menu-cell-accessory-default.png"];
            UIImage *highlighted = [UIImage imageNamed:@"menu-cell-accessory-selected.png"];
            if(indexPath.row == currentCheckedCell){
                //DEFAULT ACCESSORY CHECK 
                // cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
                UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:highlighted];
            }else{
                 //DEFAULT ACCESSORY NONE 
                // cell.accessoryType = UITableViewCellAccessoryNone;
                  UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:accessory];
             }
            [cell setAccessoryView:accessoryView];
    
        } 
    return cell;
    }
    
    - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
          //UPDATE SELECTED CELL & RELOAD TABLE
          currentCheckedCell = indexPath.row;
          [self.tableview reloadData];
    }
    

    另外值得注意的是,我的示例使用自定义图像作为配件。 如果您查看@ACB 提供的链接,您会发现一个非常相似的概念。

    【讨论】:

    • 我不太明白你的想法。你能把你的意思的代码放在那里吗?
    • 我根本无法在我的 tableview 中使用这种方法,它没有帮助
    【解决方案3】:

    你可以使用,

    if (firstCellSelected)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } 
    

    viewDidLoad 方法中设置firstCellSelected 标志。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多