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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13045917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:55:22  来源:igfitidea点击:

Set value of variable after selecting row and add checkmark (xcode)

xcodetableviewcheckmark

提问by TomasJ

I'd like to do following: After clicking a row in a tableview, I want to set value of some variable and in the same time change the cell accessory type to checkmark. Here is my implementation file:

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

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

采纳答案by Pulkit Goyal

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

EDITSet only the accessory for selected row in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

编辑仅设置所选行的附件- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.accessoryType = UITableViewAccessoryNone;

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

See thisto remove checkmarks from currently visible cells in tableview.

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

EDIT 2To unselect the previous row, do this in didSelectRowAtIndexPath

编辑 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;
    } 
}

回答by TomasJ

This solved my problem with removing old checkmarks:

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

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

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

and problem with setting value of current row was solved in previous answer.

设置当前行值的问题在之前的答案中已解决。