<分区>
<分区>
我正在尝试在 UITableView 中选择的行中设置复选标记(放置在弹出 View 中)。其实我有这样的东西:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Check if current row is selected
Boolean isNowChecked = NO;
if([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
isNowChecked = YES;
}
if(isNowChecked)
{
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
else
{
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
return indexPath;
}
结果我可以选择行,例如我触摸第一个项目然后我看到复选标记。当我向下滚动表格 View 时,我发现我还检查了其他项目(编号 17、32)。当我取消选中我的第一个项目时,每个选中项也会消失。
您有什么建议是造成这种情况的原因,我该如何避免?
最佳答案
//试试这个 //在.h中
NSMutableArray *arr_fortable;
//.m
- (void)viewDidLoad
{
[super viewDidLoad];
arr_fortable = [[NSMutableArray alloc] init];
for (int i =0; i<5; i++)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:@"%d",i+1] forKey:@"data"];
[dict setObject:@"0" forKey:@"checkmark"];
[arr_fortable addObject:dict];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [arr_fortable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"data"];
if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[arr_fortable objectAtIndex:indexPath.row] objectForKey:@"checkmark"] integerValue] == 1)
{
[[arr_fortable objectAtIndex:indexPath.row] setObject:@"0" forKey:@"checkmark"];
}
else
{
[[arr_fortable objectAtIndex:indexPath.row] setObject:@"1" forKey:@"checkmark"];
}
[self.tableView reloadData];
}
关于ios - 在 UITableView 中设置复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17419214/
相关文章:
ios - Google maps turn by turn GPS 在用于 iOS 开发的应用程序导航中
ios - 为任何滚动调用 UICollectionViewLayout prepareLayout
iphone - Objective C - 对 NSMutable 字符串使用 insertString
swift - TableView cellForRowAtIndexPath 警告
ios - 从 Storyboard 设置时看不到 UIImageView 图像
ios - 如何从coredata swift中随机选择一个元素