gpt4 book ai didi

iphone - 重新加载 UITableView

转载 作者:行者123 更新时间:2023-12-03 21:19:40 25 4
gpt4 key购买 nike

我有一个实现 UITableViewDelegateNSXMLParserDelegate 的类。我的应用程序是基于选项卡的。当我选择正确的选项卡时,在类的 viewWillAppear 方法中,我启动 xml 解析器来解析特定 url 处的 rss feed,然后用以下内容填充我的 UITableView提要的内容。
现在我想要一个按钮来“刷新” View (即再次解析 rss 提要并显示新结果)。因此,我将此方法作为刷新按钮的操作给出:

-(IBAction)refreshFeeds:(id)sender
{
if (stories){
[stories release]; // Stories is an NSMutableArray for storing the parsed feeds
stories = nil;
}
[self viewWillAppear:YES];
NSLog(@"RELOADING!!!!!!!!!!!!!!!");
}

我的问题是,当我按下“刷新”按钮时, View 会变成空白,就好像没有要显示的提要一样。
如果我切换到另一个选项卡然后返回,表格 View 将再次填充提要。
我究竟做错了什么?预先感谢您。

(这是我如何实现该类的一部分)

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([stories count] == 0){
NSString * path = @"http://www.theRssUrl.com";
[self parseXMLFileAtURL:path];
}
}
//custom method to parse rss xml
- (void)parseXMLFileAtURL:(NSString *)URL {

if (stories) {
[stories release];
stories = nil;
}
stories = [[NSMutableArray alloc] init];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];

rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];

[rssParser parse];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {

[newsTable reloadData]; //newsTable is the UITableView i want to refresh
self.view = newsTable;
[rssParser release];
rssParser = nil;
}

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell.
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil){
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

int storyIndex = indexPath.row;

cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];

return cell;
}

最佳答案

您可以直接调用 [self parseXMLFileAtURL:path]; 来刷新您的 Feed,因为此方法会清空数组。不要直接调用 viewWillAppear。只需在viewWillAppear 和refreshFeeds 中调用parseXMLFileAtURL 即可。

关于iphone - 重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com