У меня есть массив из 8 элементов, помещенных в табличное представление. Я использую следующий код для установки флажка при нажатии строки
[[tableView cellForRowAtIndexPath: indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
. Но моя проблема - 1] всякий раз, когда я касаюсь своей первой строки, также появляется флажок пятой строки.И 2], когда я прокручиваю табличное представление после проверки строки и возвращаюсь назад, отмеченная отметка исчезает.Как избавиться от этих проблем.заранее спасибо
вот код
- (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;
}