【问题标题】:UITableView didselectrowatindexpath not getting calledUITableView didselectrowatindexpath 没有被调用
【发布时间】:2023-04-01 18:00:02
【问题描述】:

由于某种原因,我的 UITableView 委托方法 didSelectRowAtIndexPath 在我选择行之前不会被调用。此外,虽然我将 UITableView 的编辑样式设置为 UITableViewCellEditingStyleDelete,但当我在 tableview 上滑动手指时,它不会显示删除按钮。我已将情节提要中的 tableview 的委托和数据源属性设置为我的视图控制器,但委托方法仍然没有被正确调用。单元格仍然起作用并将导航到我的其他详细信息视图,但我只是得到了一些非常奇怪的行为。这是我用于表格视图的代码:

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [_lists count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MasterListCell";

    /* Set up list cell */
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    CGRect myImageRect = CGRectMake(0.0f, 0.0f, 15.0f, 15.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"cell-arrow.png"]];

    cell.accessoryView = myImage; //cellArrowNotScaled;
    cell.editingAccessoryType = UITableViewCellEditingStyleDelete;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    /* Define a new List */
    List *list = [_lists objectAtIndex:indexPath.row];
   // cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.textLabel.font = [UIFont fontWithName:@"Roboto-Medium" size:15];
    cell.textLabel.text = list.name;



    cell.textLabel.highlightedTextColor = [UIColor blackColor];

    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:

    (NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"This list will be permanently deleted." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
        [alert show];

    }
}


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
    if (currentSelectedIndexPath != nil)
    {
        [[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:[UIColor yellowColor]];
    }

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did select row");
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor yellowColor]];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell.isSelected == YES)
    {
        [cell setBackgroundColor:[UIColor yellowColor]];
    }
    else
    {
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
}

【问题讨论】:

  • 是否有任何个委托方法被调用?这是您的代码的复制粘贴吗? (我问这个是因为UITableView didSelectRowAtIndexPath: not being called on first tap。)
  • 你声明视图控制器遵循 UITableViewDataSource 和 UITableViewDelegate 协议了吗?
  • @JoshCaswel 实际上对你们俩都是的
  • 您说“在我选择行之前,不会调用方法 didSelectRowAtIndexPath”。您的意思是比您预期的稍晚才打来电话?
  • 您最好在单元子类中实现自定义突出显示颜色,而不是在视图控制器中。此外,您似乎对突出显示感兴趣,而不是选择,因此您应该使用tableView:didHighlightRowAtIndexPath: / tableView:didUnhighlightRowAtIndexPath:(但同样:这不是一个好方法)。

标签: ios uitableview


【解决方案1】:

更新答案

从您下面的评论中,我明白您的意思。您正在尝试通过选择突出显示来伪造分组样式的自定义选定背景(如果不提供自定义图像就无法自定义),而是在点击单元格时设置未选定的背景颜色。你可以在shouldHighlightRowAtIndexPath

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return YES;
}

此方法在didSelectRowAtIndexPath 之前被调用,即使选择样式为无。当单元格不应该被突出显示时,您需要详细说明上述解决方案以设置颜色。

原答案

在我选择行之前不会调用didSelectRowAtIndexPath

这是设计使然,因此名称中使用了过去时“did”。

此外,虽然我将 UITableView 的编辑样式设置为 UITableViewCellEditingStyleDelete,但当我在表格视图上滑动手指时,它不会显示删除按钮。

您必须实现数据源方法tableView:commitEditingStyle:forRowAtIndexPath: 才能显示删除按钮。如果你仔细想想,这是有道理的。您没有为数据源提供响应编辑的方法,因此 iOS 得出结论认为它不应该编辑。

我是说当我按下带有 UITableViewSelectionStyleBlue 的单元格时,它显示为蓝色。但是,当我将其设置为 none 并尝试更改 didSelectRowAtIndexPath 中的背景颜色时,直到我将手指从单元格上抬起后它才会改变。当我放下手指而不抬起手指时,我希望单元格的背景颜色发生变化

你最终想要完成什么?听起来您想做自定义突出显示颜色。这样做的方法是将单元格的 selectedBackgroundView 替换为您自己的视图并设置该视图的背景颜色:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //...
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
    cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];
    //...
}

如果这不是你想要的,请澄清,我会更新我的答案。

【讨论】:

  • cell.selectedBackgroundView 不会为分组的 tableview 剪切它。它适用于非分组。另外,我不知道你有没有看过我上面的代码,但我确实实现了那个委托方法......
猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 2011-01-07
  • 2013-10-07
  • 1970-01-01
  • 2011-03-04
相关资源
最近更新 更多