【问题标题】:Why am I not able to run reloadData?为什么我无法运行 reloadData?
【发布时间】:2012-03-21 17:39:04
【问题描述】:

这是UIViewController。我想在我的表格视图上运行reloadData,但它不起作用。

首先,我从另一个角度调用fromGenre:。我开始NSURLConnection。然后它调用connectionDidFinishLoading:。然后doParse:。之后,在这个函数中,我想调用reloadData。但它没有运行。

- (void)viewDidLoad{
[super viewDidLoad];    
}

//Parse function
-(void)doParse{   
    NSString *jsonString = [[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
    data2=[[[[jsonString JSONValue]] objectForKey:@"feed"] objectForKey:@"entry"];
     trackGenre = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in data2) {
        NSString *artistName = [[dic objectForKey:@"im:artist"]objectForKey:@"label"];
         NSMutableDictionary *dicData = [NSMutableDictionary dictionary];
        [dicData setValue:artistName forKey:@"artist"];
        [trackGenre addObject:dicData];
    }
    **//[self.aTableView reloadData];
    /*
     [ self.aTableView performSelectorOnMainThread:@selector( reloadTableView )
                                       withObject:nil
                                    waitUntilDone:YES
     ];*/
    [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
    //[self reloadTableView];**
}

-(void)reloadTableView{
[[[self view] aTableView] reloadData];
NSLog(@"do reload");
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//[self doParse];
[self performSelectorOnMainThread:@selector(doParse) withObject:nil waitUntilDone:YES];    
}


-(void)fromGenre:(NSURLRequest *)urlRequest{
NSLog(@"urlRequest:%@",urlRequest);
aConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return trackGenre ? [trackGenre count]:100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"second_VC_Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.textLabel.text = [[trackGenre objectAtIndex:indexPath.row]objectForKey:@"track"];        
return cell;
}

@end

【问题讨论】:

  • 你的问题真的不清楚...你能更好地解释你的问题是什么吗?
  • 我的问题是我该怎么做才能重新加载tableView。就是这样。
  • 不应该将reloadData 调用到self.aTableView,而不是[[self view] aTableView]?所以[self.aTableView reloadData]
  • do reload 消息打印出来了吗?如果是您没有正确引用表格视图。如果不是你有线程问题。
  • 我描述了错误的命令。我已经正确运行 [self.aTableView reloadData] 或 [aTableView reloadData]。但这也不起作用。(“重新加载”消息也打印出来。)我尝试将 [NSLog] 写入 [cellForRowAtIndexPath:] 和 [tableView numberOfRowsInSection:]。 [tableView numberOfRowsInSection:] 已被调用。然而,[CellForRowAtIndexPath:] 似乎没有被调用。

标签: iphone objective-c uitableview uiviewcontroller reloaddata


【解决方案1】:

您应该尝试使用调试器。 [[self view] aTableView] 似乎是引用表格视图的一种奇怪方式。如果它是控制器的属性,则应使用self.aTableView,如果是常规实例变量,则应仅使用aTableView

-(void)reloadTableView{
    [aTableView reloadData];
    NSLog(@"do reload");
}

【讨论】:

  • 我描述了错误的命令。我已经正确地编写了 [self.aTableView reloadData] 或 [aTableView reloadData]。但这也不起作用。
【解决方案2】:

所以.. reload 被调用.. 然后按照 erik 的建议做并使用调试器:) ppl 往往使用得不够多:P 在重新加载之前设置一个断点并查看变量:

po 流派跟踪

po一个TableView

po aTableView.dataSource

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 2012-09-17
    • 2015-03-24
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多