默认 CellAccessory [英] Default CellAccessory

查看:26
本文介绍了默认 CellAccessory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I set first row of tableview checkmarked when the app started? I mean when I start app now, there are few rows in tableview and when I click on some of them its accessory change to checkmark. When click another one, the another one comes checkmarked and previous checkmark dissapears. So now I want first row of tableview to be checkmarked when app is started and then when I click another row it 'uncheckmark' and 'checkmark' new row etc...

推荐答案

尝试这些帖子中建议的选项 如何使用目标 c 在 iphone 中的表格视图上应用复选标记?iPhone :UITableView CellAccessory Checkmark

Try the options suggested in these posts how to apply check mark on table view in iphone using objective c? or iPhone :UITableView CellAccessory Checkmark

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

Basically you need to keep track of the checkmarks using say a dictionary or so. And In viewDidLoad or init method make the first cell as checked in the dictionary. While drawing cells, always check if the corresponding entry in the dictionary is checked or not and display check mark accordingly. When user taps on a cell, modify the value in dictionary to checked/unchecked.

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

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

In .h file declare a property as

@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];
}

这篇关于默认 CellAccessory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆