Убедитесь, что ваш tableView допускает выбор:
myTableView.allowsSelection = YES;
Определите два свойства, два хранят первый и второй пути индекса выбора:
@property (nonatomic, retain) NSIndexPath *firstSelection;
@property (nonatomic, retain) NSIndexPath *secondSelection;
Установите выборвсякий раз, когда пользователь выбирает строку.В этом примере я использую подход FIFO к выборкам.Кроме того, если уже сделано два выбора, покажите атрибуты объекта:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// push the selections up each time. The selected items will always be the
// last two selections
self.firstSelection = self.secondSelection;
self.secondSelection = indexPath;
// if both selections are not nil, two selections have been made.
if (self.firstSelection && self.secondSelection)
[self showComparisonOfObject:self.firstSelection
withObject:self.secondSelection];
}
Наконец, используйте аксессуар с галочкой в выбранных строках:
- (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];
}
cell.textLabel.text = someTextYouDefine;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// this is where the magic happens
BOOL cellSelected = indexPath == self.firstSelection ||
indexPath == self.secondSelection;
cell.accessoryType = cellSelected ? UITableViewCellAccessoryCheckmark :
UITableViewCellAccessoryNone;
// the following two lines ensure that the checkmark does not cause
// the label to be off-center
cell.indentationLevel = cellSelected ? 1 : 0;
cell.indentationWidth = 20.0f;
return cell;
}