【问题标题】:reloading a tableview when source is NSURLConnection and gets new data当源为 NSURLConnection 并获取新数据时重新加载表格视图
【发布时间】:2010-10-22 23:08:59
【问题描述】:

我使用 uitableview 的 tabController 应用程序获取 NSURLconnection 数据并尝试显示它。我无法弄清楚这个问题。如果我用数组替换连接,我将存储所有向下钻取的工作,但更新不会,所以我认为 tableView 的结果很好。但是:

如果我把 [[self.tableview] reloadData];在 -(void) connectionDidFinishLoading:

这会导致我的父行在视图上出现循环。当您选择一行时,它会重新填充同一行“标题”,而不是它的子行。如果我删除 [[self.tableview] reloadData]; tableDataSource3 是空的,没有用 URL 数据刷新,所以我需要它。我有 NSLogs 显示更新,所以我知道它正在更新。

如果我把 [[self.tableview] reloadData];在 ViewDidAppear 中:

在我移出表格并返回之前,视图是空的(该应用程序是一个带有 tableViews 的 tabcontroller)。如果我在数据库中添加一行并通过 NSURL 连接进行更新,tableView 将保持原样,但如果我选择一行,则更新的新数据将出现在推送的表中。

如果我把 [[self.tableview] reloadData];在 ViewWillAppear 中:

在我离开桌子并返回之前,视图是空的。但是,推送的子视图是空的。

有什么想法吗?

NSURLconnection方法到此结束

    -(void) connectionDidFinishLoading:(NSURLConnection *)conn3 {

 ~ JSON STUFF~

  NSArray *test = [json objectForKey:@"Rows"];
  tableDataSource3 = [[NSArray alloc] initWithArray:test];


  [json release];
  [conn3 release];
  self.receivedData =nil;
  [[self tableview] reloadData];
    }

这里是选择方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  //Get the dictionary of the selected data source.
 NSDictionary *dictionary = [self.tableDataSource3 objectAtIndex:indexPath.row];

 //Get the children of the present item.
 NSArray *Children = [dictionary objectForKey:@"Children"];

 if([Children count] == 0) {

  ItemDetailViewController *dvController = [[ItemDetailViewController alloc] initWithNibName:@"ItemDetailView" bundle:[NSBundle mainBundle]];
  [self.navigationController pushViewController:dvController animated:YES];
  [dvController release];
 }
 else {

   //Prepare to tableview.
  ThirdTab *rvController = [[ThirdTab alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];


  rvController.CurrentLevel3 += 1;


  rvController.CurrentTitle3 = [dictionary objectForKey:@"Title"];
  [self.navigationController pushViewController:rvController animated:YES];

  rvController.tableDataSource3 = Children;

  [rvController release];
 }

}

【问题讨论】:

    标签: iphone uitableview nsurlconnection


    【解决方案1】:

    有点难以追踪你在说什么......而且你的代码需要格式化(上图)

    试试……

    • 在 -viewDidLoad 中启动您的 NSURLConnection 请求
    • [[self tableView] reloadData]; 只能在 connectionDidFinishLoading: 中调用,或者在 NSURLConnection 请求之外对数据数组进行任何更新之后调用。
    • connectionDidFinishLoading:self.tableDataSource3 = [[NSArray alloc] initWithArray:test]; before the [[self tableView] reloadData];

    【讨论】:

    • @Jordan 是的,NSURL 从 viewDidLoad 开始
    • @Jordan 我在 connectionDidFinishLoading 结束时只有一次 reloadData。添加自我是个好主意,但我得到了相同的结果。谢谢
    • 我感觉整个 didselectRowatIndexPath 对于 reloadData 调用是不正确的。我发布了一个新问题。
    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多