我有多個對象,每個對象都有一系列屬性。我希望用戶選擇兩個對象,然後應用程序將顯示各個對象屬性的比較。我很難決定如何最好地選擇這兩個對象。我會使用UITableView,但如何在繼續之前選擇兩個單元格?或者,我可以顯示UIButtons,但是又一次,在繼續之前如何選擇最好的兩個?也許還有另一種方式,我沒有發現。iphone SDK:選擇兩個對象的最佳方法(作比較)
意見讚賞。
我有多個對象,每個對象都有一系列屬性。我希望用戶選擇兩個對象,然後應用程序將顯示各個對象屬性的比較。我很難決定如何最好地選擇這兩個對象。我會使用UITableView,但如何在繼續之前選擇兩個單元格?或者,我可以顯示UIButtons,但是又一次,在繼續之前如何選擇最好的兩個?也許還有另一種方式,我沒有發現。iphone SDK:選擇兩個對象的最佳方法(作比較)
意見讚賞。
確保您的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;
}
您可以使用UITableView
並使用複選標記accessoryView
,然後在選擇兩個對象後有一個「繼續」按鈕。然而,如果你想要確保兩個對象(不再)被選中,那麼你可以使用兩個UIPicker
,一個在另一個之上,這樣用戶可以選擇這兩個對象。
感謝AnswerBot。讚賞。 – Jeremy
AnswerBot很樂意回答。 – memmons