【发布时间】:2011-01-04 16:44:18
【问题描述】:
我有一个 UITableView,其中包含一个故事列表和一个底部的单元格,用于加载更多故事。我正在尝试取消选择“更多故事...”单元格并将其文本更改为“正在加载...”。我已经在整个互联网和 stackoverflow 上进行了搜索,但我无法弄清楚为什么我的代码不能正常工作。现在,当单击“更多故事...”单元格时,它会保持选中状态并且永远不会更改其文本。
对于那些询问的人,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];
}
}
已更新,但仍无法正常工作。现在它最终取消选择单元格并将文本更改为“正在加载...”,但只有在 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];
}
}
【问题讨论】:
-
在后台线程中执行 moreStories。像 [self performSelectorInBackground:@selector(moreStories:) withObject:nil];然后,您需要在 moreStories 方法中创建一个新的 NSAutoReleasePool 并在方法结束时将其排空。
标签: ios objective-c iphone uitableview cocoa-touch