Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableViewCell在选择时选中标记更改_Objective C_Iphone_Tablecell_Checkmark - Fatal编程技术网

Objective c UITableViewCell在选择时选中标记更改

Objective c UITableViewCell在选择时选中标记更改,objective-c,iphone,tablecell,checkmark,Objective C,Iphone,Tablecell,Checkmark,我认为要将“开”的复选标记更改为“关”,我必须在didSelectRowAtIndexPath上的CellAccessoryType和checkmark之间更改 因为我已经这样做了,但我注意到这种行为与iphone上自动锁定设置上的复选标记单元格并不完全相同 还是有其他方法来处理复选标记 在视图控制器中保留一个名为selectedRow的属性,该属性表示一行的索引,该行表示表节中的选中项 在视图控制器的-tableView:cellforrowatinexpath:委托方法中,如果单元格的ind

我认为要将“开”的复选标记更改为“关”,我必须在
didSelectRowAtIndexPath
上的
CellAccessoryType
checkmark
之间更改

因为我已经这样做了,但我注意到这种行为与iphone上自动锁定设置上的复选标记单元格并不完全相同

还是有其他方法来处理复选标记

  • 在视图控制器中保留一个名为
    selectedRow
    的属性,该属性表示一行的索引,该行表示表节中的选中项

  • 在视图控制器的
    -tableView:cellforrowatinexpath:
    委托方法中,如果单元格的
    indexPath.row
    等于
    selectedRow
    值,则将
    单元格的
    accessoryType
    设置为
    UITableViewCellAccessoryCheckmark
    。否则,将其设置为
    UITableViewCellAccessorOne

  • 在视图控制器的
    -tableView:didselectedrow=indexPath:
    委托方法中,将
    selectedRow
    值设置为选中的
    indexPath.row
    ,例如:
    self.selectedRow=indexPath.row

  • 应该是

    didHighlightRowAtIndexPath

    而不是

    tableView:didSelectRowAtIndexPath


    Alex answer只在添加重新加载表后才为我工作, in.h

    {
      int selectedCell;
    }
    @property(nonatomic,readwrite)int selectedCell;
    
     self.selectedCell = indexPath.row;
        [self.tableView reloadData]; 
    
    in.m *cellForRowAtIndexPath*

    并在任意位置选择HighlightRowatineXpath或选择RowatineXpath

    {
      int selectedCell;
    }
    @property(nonatomic,readwrite)int selectedCell;
    
     self.selectedCell = indexPath.row;
        [self.tableView reloadData]; 
    
    另一个解决方案:

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
        [self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        return indexPath;
    }
    
    是的:您不必检查
    oldIndex
    是否为
    nil
    :)


    Swift 3及更高版本:

    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if let oldIndex = tableView.indexPathForSelectedRow {
            tableView.cellForRow(at: oldIndex)?.accessoryType = .none
        }
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    
        return indexPath
    }
    

    Zyphrax提出了一个对我非常有效的解决方案!如果需要清除上一选定行,只需使用:

    [self.tableView reloadData]; 
    


    如果要将自定义图像用作附件类型,请使用下面的代码

    -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_icon.png"]];
        [imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)];
        imgCheckMark.tag = 1000+indexPath.row;
    
        NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow];
        [self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone;
        [self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark;
        return indexPath;
    }
    
    斯威夫特:

    然后:

    斯威夫特

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
      }
    
      func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
      }
    

    对不起,我不是说我有多个复选标记单元格(更像是单选按钮)。只是它们的动画没有那么流畅(但我现在已经修复了)。但是你的回答意味着我正在使用正确的方法:)添加到Alex的解决方案中-为了在点击单元格时显示复选标记,你还必须在设置所选单元格后输入[tableview reloadData]。这很简单,但将数据重新加载到整个表中可能会影响性能。因为您知道需要更新什么
    indexath.row
    ,所以只需要更新该行,这可以通过表视图方法
    -reloadRowsAtIndexPaths:withRowAnimation:
    完成。有关更多详细信息:@AlexReynolds,如果单元格的indexPath.row等于selectedRow值,请详细说明您何时说过将单元格的accessoryType设置为UITableViewCellAccessoryCheckmark。否则,将其设置为UITableViewCellAccessoryNone。该行需要或不需要复选标记。如果是所选行(通过比较索引路径行和所选行值),则标记它。否则,不要这样做。这真是一个伟大的代码。我添加了Swift版本。非常好的解决方法。非常感谢。这是最直接和正确的答案。
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
        self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
    }
    
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
      }
    
      func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
      }