选择行后设置变量值并添加复选标记(xcode)

时间:2012-10-24 08:58:58

标签: xcode tableview checkmark

我想做以下操作:点击表格视图中的一行后,我想设置某个变量的值,同时将单元格附件类型更改为复选标记。这是我的实现文件:

- (void)viewDidLoad
{
    [super viewDidLoad];
    tabledata = [[NSArray alloc] initWithObjects:@"row1", @"row2", @"row3", nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tabledata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Row"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Row"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark TableVoew Delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if row1 is selected change 'value' to 1 and change cell accessory to checkmark (other cells accessory to none)
//if row2 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
//if row3 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
}

2 个答案:

答案 0 :(得分:0)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.value = indexPath.row;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

修改 仅在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中设置所选行的附件
cell.accessoryType = UITableViewAccessoryNone;

if (indexPath.row == self.value) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

请参阅this以删除tableview中当前可见单元格的复选标记。

编辑2 要取消选择上一行,请在didSelectRowAtIndexPath

中执行此操作
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(@"Oldindexpath is: %@", oldIndexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

if (newCell.accessoryType == UITableViewCellAccessoryNone) {
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];

if (oldCell != newCell){
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    } 
}

答案 1 :(得分:0)

这解决了删除旧复选标记的问题:

for (UITableViewCell *cell in [tableView visibleCells]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

在前一个答案中解决了当前行设置值的问题。