【发布时间】: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