4

在启用选择的情况下使用UITableView时,我可以选择一行,并通过突出显示选中它。但是,当我选择第二行时,默认情况下会发生这种情况:

  1. 第 1 行已被选中并明显突出显示。
  2. 我将手指按在第 2 行。
  3. 当我的手指仍然按下时,第 1 行和第 2 行都明显突出显示。
  4. 现在松开我的手指会选择第 2 行,并且只有它明显突出显示。

我要做的是在上面的第 3 步中做到这一点,两个单元格不会同时突出显示。是否有可能做到这一点?

4

4 回答 4

1

在此使用此委托,您可以取消选择单元格。

  • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
于 2018-10-08T10:00:25.450 回答
0

按着这些次序

  • 在情节提要中选择表格视图
  • 在“选择”类型中选择“单选”而不是“多选”(在属性检查器中)

希望这可以帮助你

于 2014-11-13T07:32:54.950 回答
0

这有效

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
return NO;

}

于 2014-11-13T07:02:50.863 回答
-1

好的,我根据讨论编辑了这个答案。

假设您正在继承 UITableViewCell,请在实现中使用此代码:

(例如,CustomTableCell.m)

#define MyTableCellHighlightedNotification @"MyTableCellHighlighted" 

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        // Your custom initialization here

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tableCellHighlighted:)
                                                     name:MyTableCellHighlightedNotification
                                                   object:nil];

    }
}

- (void) dealloc
{
    [[NSNotifcationCenter defaultCenter] removeObserver:self];

    // ...Release ivars...

    [super dealloc]
}

- (void) setHighlighted:(BOOL) highlighted
{
    // Default behaviour (defer to super)
    [super setHighlighted:highlighted];

    if(highlighted == YES){
        // De-highlight all other cells
        [[NSNotificationCenter defaultCenter] postNotificationName:MyTableCellHighlightedNotification
                                                            object:self]

    }
}

- (void)tableCellHighlighted:(NSNotification*) notification
{
    // All cells receive this notification

    if([notifcation object] != self){
        // All cells except the notification sender de-highlight themselves
        [self setHighlighted:NO];
    }
}
于 2012-06-15T21:10:32.400 回答