0
我有一個UITableView的部分自定義使用addSubview的單元格。我爲最後一個單元使用不同的單元ID,其目的是從服務器加載更多數據,這會使新單元出現。 (想象Mail.app中的「從服務器加載更多消息」單元)爲什麼UITableViewCell選擇不能繪製,如果我很快點擊?
例如,
- (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是否是表的最後一行有關。
任何想法如何讓它每次都畫出亮點?
我不希望它處理任何東西,我只是希望單元格選擇始終顯示。我知道它會不響應。 – Jeremy 2009-08-12 20:05:09
如果你的代碼在主線程中被阻塞,那麼UI線程需要做的更新和響應用戶輸入的任何處理都不會發生,除了使用線程之外,還沒有真正的解決方法。 – 2009-08-12 20:42:24
這是一個很好的觀點。我想我應該嘗試一些異步的東西。 – Jeremy 2009-08-13 15:15:27