如果我快速点击,为什么UITableViewCell选择不能绘制?

时间:2009-08-12 15:56:28

标签: iphone objective-c cocoa-touch uitableview

我有一个UITableView,其中包含使用addSubview进行部分自定义的单元格。我正在为最后一个单元使用不同的单元ID,其目的是从服务器加载更多数据,这将使新单元格出现。 (想想Mail.app中的“从服务器加载更多邮件”单元格)

E.g。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // <... code for normal cells omitted...>
    static NSString *LoadMoreCellIdentifier = @"LoadMoreCellIdentifier";

    UILabel *loadMoreLabel;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:LoadMoreCellIdentifier] autorelease];

        cell.selectionStyle = UITableViewCellSelectionStyleGray;

        loadMoreLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, self.tableView.rowHeight - 1)] autorelease];
        loadMoreLabel.tag = LOAD_MORE_TAG;
        loadMoreLabel.font = [UIFont boldSystemFontOfSize:16.0];
        loadMoreLabel.textColor = [UIColor colorWithRed:0.153 green:0.337 blue:0.714 alpha:1.0]; // Apple's "Load More Messages" font color in Mail.app
        loadMoreLabel.textAlignment = UITextAlignmentCenter;
        [cell.contentView addSubview:loadMoreLabel];
    }
    else
    {
        loadMoreLabel = (UILabel *)[cell.contentView viewWithTag:LOAD_MORE_TAG];
    }

    loadMoreLabel.text = [NSString stringWithFormat:@"Load Next %d Hours...", _defaultHoursQuery];
    return cell;
}

如上所示,我设置了cell.selectionStyle = UITableViewCellSelectionStyleGray;

当你点击一个单元格时,我会清除这样的选择:

-       (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if (_showLoadMoreEntriesButton && indexPath.row == [_sourceArray count])
    {
         // <... omitted...>
         // Do a potentially blocking 1 second operation here to obtain data for more
         // cells. This will potentially change the size of the _sourceArray 
         // NSArray that acts as the source for my UITableCell
         [tableView deselectRowAtIndexPath:indexPath animated:NO];

         [self.tableView reloadData];
         return;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self _loadDataSetAtIndex:indexPath.row];
}

我看到的问题是我必须轻拍并按住我的手指以显示灰色突出显示。如果我快速点击,它根本不会显示高亮显示。问题是有时候我会执行一个大约需要一秒钟的阻塞操作。我想要一些简单的UI反馈,而不是只是锁定的UI。我认为这可能与我有条件地检查indexPath是否是表的最后一行有关。

有没有想过如何让它每次都能画出亮点?

1 个答案:

答案 0 :(得分:1)

如果可能,将阻止操作移动到另一个线程。 NSOperation可能是一个很好的系统。

当你处于阻塞操作过程中时,没有干净的方法告诉UI线程处理任何事情。