gpt4 book ai didi

iphone - 在表格 View 中使用附件复选标记

转载 作者:行者123 更新时间:2023-12-03 21:18:46 25 4
gpt4 key购买 nike

我在表格 View 中放置了 8 个项目的数组。我使用以下代码在点击一行时设置复选标记

[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

.但我的问题是1]每当我点击第一行时,第五行复选标记也会出现。和2]当我在检查一行后滚动 TableView 并返回时,检查标记消失。如何摆脱这些问题。提前致谢

这是代码

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

[tableView deselectRowAtIndexPath:indexPath animated:NO];
// find the cell being touched and update its checked/unchecked image
CellView *targetCustomCell = (CellView *)[tableView cellForRowAtIndexPath:indexPath];
[targetCustomCell checkAction:nil];

if ([arrData count]>0)
{
if ([arrData containsObject:[arrName objectAtIndex:indexPath.row]]) {
[arrData removeObject:[arrName objectAtIndex:indexPath.row]];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
NSLog(@"data removed");
NSLog(@"arrData%@",arrData);
}
else {
[arrData addObject:[arrName objectAtIndex:indexPath.row]];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(@"data added");
NSLog(@"tableArray%@",arrData);
}

}
else {
[arrData addObject:[arrName objectAtIndex:indexPath.row]];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(@"data added");
NSLog(@"arrData%@",arrData);
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"MyCellID";

CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CellView *)[[[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.text = [arrName objectAtIndex:indexPath.row];
return cell;
}

最佳答案

对于您的1)问题,您提供了一些代码

2)问题:

您需要记住最后选择的行,因为您需要通过以下方式存储最后选择的索引号:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
selectedIndex = indexPath.row;
[tableView reloadData];
}

在你的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (indexPath.row == selectedIndex)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.accessoryType = UITableViewCellAccessoryNone;

return cell;
}

希望对你有帮助。

关于iphone - 在表格 View 中使用附件复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6981952/

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