【问题标题】:Why doesn't UITableViewCell selection not draw if I tap very quickly?如果我快速点击,为什么 UITableViewCell 选择不绘制?
【发布时间】:2009-08-12 15:56:27
【问题描述】:

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

例如

- (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 是否是表的最后一行有关。

知道如何让它每次都画出高光吗?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案1】:

    如果可能,请将您的阻塞操作移至另一个线程。 NSOperation 可能是一个很好用的系统。

    当您处于阻塞操作的中间时,没有明确的方法可以告诉 UI 线程处理任何内容。

    【讨论】:

    • 我不希望它处理任何东西,我只希望单元格选择始终显示。我知道它会无响应。
    • 如果您的代码在主线程上阻塞,那么 UI 线程需要执行的更新和响应用户输入的任何处理都不会发生,除了使用线程。
    • 这很好。我想我应该尝试一些异步的。
    • 为了看看这是否是问题所在,我完全删除了阻塞代码。我现在要做的就是取消选择该行。即便如此,如果我快速触摸,高光不会绘制。它似乎在我的其他“正常”唯一单元格标识符上正确绘制,但不是在我的“加载更多”单元格上,我已经发布了上面的代码。还有其他想法吗?感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2021-08-09
    • 2014-08-02
    • 1970-01-01
    相关资源
    最近更新 更多