UITableViewCell 复选标记在选择时发生变化

发布于 2024-09-01 09:07:13 字数 244 浏览 8 评论 0原文

我是否正确地认为要将“on”的复选标记更改为“off”,我必须更改 nonecheckmark on 之间的 CellAccessoryType didSelectRowAtIndexPath

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

或者还有其他处理复选标记的方法吗?

Am I correct in thinking that to change the checkmark for "on" to "off", I must change the CellAccessoryType between none and checkmark on the didSelectRowAtIndexPath?

Because I have done this but I have noticed the behaviour is not perfectly identical to like the checkmark cells on the auto lock settings on the iphone.

Or is there some other way checkmarks are meant to be handled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

北城挽邺 2024-09-08 09:07:13
  1. 在视图控制器中保留一个名为 selectedRow 的属性,它表示代表表部分中选中项的行的索引。

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

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

  1. Keep a property in your view controller called selectedRow, which represents the index of a row that represents the checked item in a table section.

  2. In your view controller's -tableView:cellForRowAtIndexPath: delegate method, set the accessoryType of the cell to UITableViewCellAccessoryCheckmark if the cell's indexPath.row equals the selectedRow value. Otherwise, set it to UITableViewCellAccessoryNone.

  3. In your view controller's -tableView:didSelectRowAtIndexPath: delegate method, set the selectedRow value to the indexPath.row that is selected, e.g.: self.selectedRow = indexPath.row

醉生梦死 2024-09-08 09:07:13

另一个解决方案:

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

Another solution:

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

And yeah: you don't have to check if oldIndex is nil :)


Swift 3 and later:

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
}
少钕鈤記 2024-09-08 09:07:13

迅速:

var selectedCell:UITableViewCell?{
    willSet{
        selectedCell?.accessoryType = .None
        newValue?.accessoryType = .Checkmark
    }
}

那么:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}

swift:

var selectedCell:UITableViewCell?{
    willSet{
        selectedCell?.accessoryType = .None
        newValue?.accessoryType = .Checkmark
    }
}

then:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
┼── 2024-09-08 09:07:13

为了迅速

  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
  }

For swift

  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
  }
叹梦 2024-09-08 09:07:13

Zyphrax 提出了一个非常适合我的解决方案!如果您需要清除先前选定的行,只需使用

[self.tableView reloadData]; 

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

Zyphrax suggested a great solution that worked great for me! And if you need to clear the previous selected row, just use:

[self.tableView reloadData]; 

in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
还不是爱你 2024-09-08 09:07:13

应该是

didHighlightRowAtIndexPath

而不是

tableView:didSelectRowAtIndexPath

It should be

didHighlightRowAtIndexPath

instead of

tableView:didSelectRowAtIndexPath

腹黑女流氓 2024-09-08 09:07:13

亚历克斯的回答只有在添加重新加载表后才对我有用,
以 .h 表示

{
  int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;

以 .m 表示
*cellForRowAtIndexPath*

if(indexPath.row == selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

以及任何地方的 didHighlightRowAtIndexPath 或 didSelectRowAtIndexPath

 self.selectedCell = indexPath.row;
    [self.tableView reloadData]; 

Alex answer worked for me only after adding reload table ,
in .h

{
  int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;

in .m
*cellForRowAtIndexPath*

if(indexPath.row == selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

and in anywhere didHighlightRowAtIndexPath or didSelectRowAtIndexPath

 self.selectedCell = indexPath.row;
    [self.tableView reloadData]; 
若沐 2024-09-08 09:07:13

如果您想使用自定义图像作为accessoryType,请使用以下代码

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

If you want to use your custom image as accessoryType use below code

-(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文