我有多个对象,每个对象都有一系列属性。我希望用户选择两个对象,然后应用程序将显示各个对象属性的比较。我很难决定如何最好地选择这两个对象。我会使用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