UITableViewCell附件类型检查点击并设置其他未选中

Sag*_*ari 29 iphone xcode objective-c uitableview

我对设置表视图单元配件有点困惑.

我在表格中修了两个部分

  • 办公室

我想要的是如下......

  • 当用户点击任何一个单元格时
  • 细胞被选中&
    • 我想设置检查(设置uitableviewcell附件类型 - 检查tapped cell)
  • 此外,所有其他电池的配件类型现在应该设置为
    • uitable view cell accessory type none

我试过以下代码.但我发现indexpath.row和indexpath.section是readonly属性.

// Override to support row selection in the table view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    [tblProfileView deselectRowAtIndexPath:indexPath animated:YES];
    int i,max;
    UITableViewCell *x; NSIndexPath *tt;

    for(i=0;i<[tblProfileView numberOfRowsInSection:0];i++)
    {
        tt.row=i; tt.section=0;
        x=[tblProfileView cellForRowAtIndexPath:];
        [x setAccessoryType:UITableViewCellAccessoryNone];
    }
    for(i=0;i<[tblProfileView numberOfRowsInSection:1];i++)
    {
        tt.row=i; tt.section=1;
        x=[tblProfileView cellForRowAtIndexPath:tt];
        [x setAccessoryType:UITableViewCellAccessoryNone];
    }

    x=[tblProfileView cellForRowAtIndexPath:indexPath];
    [x setAccessoryType:UITableViewCellAccessoryCheckmark];
    // Navigation logic may go here -- for example, create and push another view controller.
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
    // [self.navigationController pushViewController:anotherViewController animated:YES];
    // [anotherViewController release];
}
Run Code Online (Sandbox Code Playgroud)

ger*_*ry3 51

我会跟踪应该检查的数据并更改tableView中的单元格:didSelectRowAtIndexPath:并更新tableView中检查的数据:cellForRowAtIndexPath:像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the data from the IndexPath.row

    if (data == self.checkedData)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row

    if (data != self.checkedData) {
       self.checkedData = data;
    }

    [tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)


pal*_*aja 17

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

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


}
Run Code Online (Sandbox Code Playgroud)

您还需要删除cellForRowAtIndexPath上的复选标记附件

if ([selectedOptionsArray indexOfObject:cell.textLabel.text] != NSNotFound) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
Run Code Online (Sandbox Code Playgroud)


iKu*_*hal 15

声明一个名为int的变量prev并实现此方法: -

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];

   if (cell.accessoryType==UITableViewCellAccessoryNone) 
   {
      cell.accessoryType=UITableViewCellAccessoryCheckmark;
      if (prev!=indexPath.row) {
         cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prev inSection:0]];
         cell.accessoryType=UITableViewCellAccessoryNone;
         prev=indexPath.row;
     }

 }
 else{
     cell.accessoryType=UITableViewCellAccessoryNone;
 }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*nna 6

仅在选定的行中为复选标记实现逻辑的最短,最简单的方法.....


1. 创建一个NSIndexPath的全局对象。

selectedIndexPathForCheckMark= [[NSIndexPath alloc] init];
Run Code Online (Sandbox Code Playgroud)

2. 在didSelectRowAtIndexPath ..中。

//将选定的indexPath关联到声明的对象

selectedIndexPathForCheckMark = indexPath;  
[tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

3. 在cellForRowAtIndexPath ..中

if ([selectedIndexPathForCheckMark isEqual:indexPath]) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else {
    //setAccessoryType None if not get the selected indexPath
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}
Run Code Online (Sandbox Code Playgroud)