为什么我的UITableViewCell不能取消select和更新文本?

我有一个UITableView的故事列表和单元格在底部加载更多的故事。 我正在尝试使“更多故事…”单元格取消选中,并在单击时将其文本更改为“正在加载…”。 我已经search了所有的互联网和各地的计算器,我不知道为什么我的代码不能正常工作。 现在,单击“更多故事…”单元格时,它将保持选定状态,并且不会更改其文本。

对于那些要求,moreStories添加25新的故事到表视图的底部。

原始代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; if (storyIndex == [stories count]) { UITableViewCell *moreCell = [tableView dequeueReusableCellWithIdentifier:@"more"]; if (moreCell == nil) { moreCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"more"] autorelease]; } // Set up the cell moreCell.textLabel.text = @"Loading..."; [tableView deselectRowAtIndexPath:indexPath animated:YES]; [self moreStories]; } else { NSLog(@"%@",[[stories objectAtIndex: storyIndex] objectForKey: @"link"]); webViewController *webController; webController = [[webViewController alloc] initWithURLPassed:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]]; [self.navigationController pushViewController:webController animated:YES]; [webController release]; webController =nil; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; } } 

已更新,但仍然无法正常工作。 现在,它终于取消select单元格,并将文本更改为“正在加载…”,但只有在moreStories完成下载所有故事后。 显然,我想要的文本是“加载…”,而故事正在下载1/4/10〜9pm

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; if (storyIndex == [stories count]) { UITableViewCell *moreCell = [tableView cellForRowAtIndexPath:indexPath]; moreCell.textLabel.text = @"Loading..."; [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSArray *arr = [[NSArray alloc] initWithObjects:indexPath,nil]; [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; [self moreStories]; } else { NSLog(@"%@",[[stories objectAtIndex: storyIndex] objectForKey: @"link"]); webViewController *webController; webController = [[webViewController alloc] initWithURLPassed:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]]; [self.navigationController pushViewController:webController animated:YES]; [webController release]; webController =nil; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; } } 

[tableView dequeueReusableCellWithIdentifier:@"more"]告诉表视图给你一个不同的单元格,一个在重用队列中,不在显示内容中。 此外,当没有可重用的单元可用于出队时, moreCell == nil条件会生成一个全新的单元。 此模式仅用于生成表格单元格以供显示时使用表格视图数据源。

您需要使用[tableView cellForRowAtIndexPath:indexPath]来获取实际选定的单元格。

 UITableViewCell *moreCell = [tableView cellForRowAtIndexPath:indexPath]; moreCell.textLabel.text = @"Loading..."; 

编辑:基于你的更新它看起来像,如评论中的aBitObvious说,URL加载请求阻止了UI的更新。 在更新UI时,尝试启动一个新线程并在该线程中加载数据,以便您的UI可以不受干扰地进行更新。

我继续做了一个你想要完成的简单的版本。 看看这是否有帮助。 我做到这一点的方式是我继续前进,并改变了UITableView的数据源。 你最终可能会做什么来加载更多的故事。 要记住的关键是如果你正在改变数据源,你需要重新加载tableView。

 - (void)viewDidLoad { NSArray *array = [[NSArray alloc] initWithObjects:@"Story 1", @"Story 2", @"Story 3", @"Story 4", @"Story 5", @"More Stories", nil]; self.stories = array; [array release]; [super viewDidLoad]; } 

// TableView的DataSource方法

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [stories count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease]; } cell.textLabel.text = [stories objectAtIndex:[indexPath row]]; return cell; } 

// TableView委托方法

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row] == [stories count] - 1) { //Get more stories [table deselectRowAtIndexPath:indexPath animated:YES]; NSArray *array = [[NSArray alloc] initWithObjects:@"Story 1*", @"Story 2*", @"Story 3*", @"Story 4*", @"Story 5*", @"Loading...", nil]; self.stories = array; [array release]; NSArray *arr = [[NSArray alloc] initWithObjects:indexPath,nil]; [table beginUpdates]; [table reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade]; [table endUpdates]; } else { //Call your methods to load the stories. }