为什么我的UITableViewCell不会选择和更新其文本? [英] Why won't my UITableViewCell deselect and update its text?

查看:131
本文介绍了为什么我的UITableViewCell不会选择和更新其文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView列表的故事和底部的单元格加载更多的故事。我试图使更多故事...单元格取消选择,并将其文本更改为加载...,当点击。我已经搜索了所有的互联网和所有stackoverflow,我无法弄清楚为什么我的代码不能正常工作。现在,当更多故事...单元格被点击,它保持选择,并且不改变其文本。

I have a UITableView with a list of stories and a cell at the bottom that loads more stories. I am trying to make the "More Stories..." cell deselect and change its text to "Loading..." when clicked. I have searched all over the internet and all over stackoverflow and I cant figure out why my code isnt working right. Right now, when the "More Stories..." cell is clicked, it stays selected and doesnt ever change its text.

对于那些人,moreStories增加了25个新故事到表视图的底部。

For those asking, moreStories adds 25 new stories to the bottom of the table view.

原始代码:

- (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];
}
}

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

Updated but still not working correctly. Now it finally deselects the cell and changes the text to "Loading...", but only after moreStories finishes downloading all of the stories. Obviously, I want the text to be "Loading..." while the stories are downloading 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];
}
}


推荐答案

code> [tableView dequeueReusableCellWithIdentifier:@more] 告诉表视图给你一个不同的单元格,一个在重复使用队列和正在显示的内容。此外,当没有可重用单元可用于出队时, moreCell == nil 条件生成全新单元。

[tableView dequeueReusableCellWithIdentifier:@"more"] tells the table view to give you a different cell, one that's in the reuse queue and out of what's being displayed. Moreover, the moreCell == nil condition generates a brand new cell when no reusable cell was available to dequeue. This pattern is only intended for use by the table view data source when generating table cells for display.

您需要使用 [tableView cellForRowAtIndexPath:indexPath

You need to use [tableView cellForRowAtIndexPath:indexPath] instead to get the cell that was actually selected.

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

EDIT:注释,URL加载请求阻止UI更新。更新用户界面时,请尝试启动新的主题并加载该主题中的数据,以便您的用户界面可以不受干扰地更新。

based on your update it looks like, as aBitObvious says in the comments, the URL load request is blocking the UI from being updated. As you update your UI, try starting a new thread and load the data within that thread, so that your UI can update undisturbed.

这篇关于为什么我的UITableViewCell不会选择和更新其文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆