gpt4 book ai didi

iphone - 如何显示自定义 UITableViewCellAccessoryCheckmark

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:46 25 4
gpt4 key购买 nike

我有一个需要自定义 UITableViewCellAccessoryCheckmark 的 TableView 。选择一行时复选标记显示,选择另一行时复选标记消失,然后出现在最后选择最多的 View 中。那很好用。

当我使用这一行时出现问题:

 cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"icon-tick.png" ]];

添加自定义 UITableViewCellAccessoryCheckmark。在该代码之后,UITableViewCellAccessoryCheckmark 保留在所有行上,并且在触摸另一行时不会消失。

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

int index = indexPath.row; id obj = [listOfItems objectAtIndex:index];

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

NSLog(@"%d",indexPath.row);
if (rowNO!=indexPath.row) {
rowNO=indexPath.row;
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;

cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:@"icon-tick.png" ]];

[self.tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
lastIndexPth=indexPath;
}

最佳答案

一种更简洁、更酷的方法是像这样覆盖 UITableViewCell:

- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
// Check for the checkmark
if (accessoryType == UITableViewCellAccessoryCheckmark)
{
// Add the image
self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImage.png"]] autorelease];
}
// We don't have to modify the accessory
else
{
[super setAccessoryType:accessoryType];
}
}

如果您这样做了,您可以继续使用 UITableViewCellAccessoryCheckmark,因为您的类会自动用图像替换它。

您应该只在 cellForRowAtIndexPath 方法中设置样式。像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [init subclassed cell here, dont forget to use the table view cache...]

cell.accessoryType = (rowNO != indexPath.row ? nil : UITableViewCellAccessoryCheckmark);

return cell;
}

然后,您只需更新 didSelectRowAtIndexPath 中的 rowNO 即可更新您的数据并重绘单元格,如下所示:

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

if (rowNO != indexPath.row)
{
rowNO = indexPath.row;
}

[self.tableView reloadData];

}

此外,不是使用 [self.tableView reloadData] 重新加载整个表格,您只能使用 reloadRowsAtIndexPaths 重新加载改变其样式(例如复选标记)的两个单元格.

关于iphone - 如何显示自定义 UITableViewCellAccessoryCheckmark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com