UITableView didselectrowatindexpath没有被调用

时间:2013-07-28 01:43:45

标签: ios uitableview

出于某种原因,直到我选择行之后才会调用我的UITableView Delegate方法didSelectRowAtIndexPath。此外,虽然我将UITableView的编辑样式设置为UITableViewCellEditingStyleDelete,但当我在tableview上滑动手指时,它不会显示删除按钮。我已将storyboard中tableview的委托和数据源属性设置为我的viewcontroller,但委托方法仍未正确调用。细胞仍然可以运行并导航到我的其他细节视图,但我只是得到一些非常奇怪的行为。这是我用于我的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]];
    }
}

1 个答案:

答案 0 :(得分:1)

更新回答

从下面的评论中,我看到了您的目标。您尝试通过选择突出显示来修改分组样式的自定义选定背景(无法提供自定义图像,而不是提供自定义图像),而是在点击单元格时设置未选择的背景颜色。您可以在shouldHighlightRowAtIndexPath

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

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

原始回答

  

在我选择行

之后才会调用didSelectRowAtIndexPath

这是设计的,因此过去式"做了"在名称中。

  

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

您必须实施数据源方法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];
    //...
}

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