섹션 테이블 뷰를 사용하여 사용자가 테이블에서 항목 하나를 선택하도록합니다. 사용자가 항목을 선택하면 항목 옆에 확인 표시가 나타납니다 (UITableViewCellAccessoryCheckmark
사용). 이전 선택을 한 경우 이전에 선택한 행에서 검사를 제거해야합니다.섹션 테이블에서 행에 대한 accessoryType 변경 사용자가 행을 선택한 후보기
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow || newRow == 0)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
[lastIndexPath release];
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
lastIndexPath
가 .h
파일에 개인 선언 : 여기에 내가 사용하고있는 코드입니다.
이 코드는 섹션으로 구분되지 않은 작은 목록에 유용합니다. 그러나 섹션으로 구분 된 대형 테이블에서는 다른 섹션의 행에 임의의 체크 표시가 나타납니다. cellForRowAtIndexPath
이 indexPath의 섹션을 무시하는 것과 거의 같습니다.
최소 섹션의 행 수보다 큰 행을 선택하면 코드도 충돌합니다. 당신이 여기에서 발생하는
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *itemSection = [items objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease];
}
NSArray *rowLabel = [itemSection objectAtIndex:row];
cell.textLabel.text = [rowLabel objectAtIndex:1];
NSString *detText = [rowLabel objectAtIndex:0];
detText = [detText stringByAppendingString:@" $"];
detText = [detText stringByAppendingString:[rowLabel objectAtIndex:2]];
cell.detailTextLabel.text = detText;
return cell;
}
은 'cellForRowAtIndexPath :'에 대한 코드를 참조해야합니다. 나는 당신이 셀을 dequeuing하고 재사용 식별자 (cellForRowAtIndexPath :'의 기본 구현)를 사용하고 있다고 가정하고있다. 이 경우 셀을 작성하는 코드가 표시된 셀을 템플릿으로 재사용하여 새 셀을 작성합니다. 이 문제를 해결하려면 몇 가지 작업을 수행 할 수 있습니다. "매번 cellForRowAtIndexPath :'"또는 "각 셀에 대한 참조를 유지하고'cellForRowAtIndexPath :'에 대한 호출을 회피하십시오. – ColdLogic
예 - 말하고 이것은 무작위 체크 표시의 첫 번째 문제를 해결할 수 있지만 가장 작은 섹션의 행 수보다 큰 행을 선택하면 충돌의 두 번째 문제가 해결되지 않을 것이라고 생각합니다. 여기에 cellForRowAtIndexPath 코드가 있습니다 : –
당신은 코멘트에 그 코드를 게시 할 수 없을 것입니다. 원래 게시물에 추가하십시오. – ColdLogic