2010-01-14 47 views
13

我有几个数据源一个UITableView后正常刷新。这是因为,我需要将数据与一个UISegmentedControl切换,如果我将它们添加为子视图,我不能使用状态栏滚动起来,等的UITableView是不会改变数据源

起初,我显示登录屏幕:

self.tableView.dataSource = loginTableView; 
self.tableView.delegate = loginTableView; 
[self.tableView reloadData]; 

然后,一旦用户登录,我做了以下修改索引:在segmentedControler,这是他们的个人资料的1:

self.tableView.dataSource = profileTableView; 
self.tableView.delegate = profileTableView; 
[self.tableView reloadData]; 

然而,表视图更新,但是有一点的两个数据源之间的混合。一些文本已经改变,一些重叠,而其中一些完全丢失。

有另一种方式,我应该改变数据源的一个UITableView?

Thx

回答

4

不知道为什么,这是从您发布的代码发生。

而是改变委托和数据源,换出任何伊娃表示所显示的数据:

- (NSArray*)tableData{ 

    if(showingLogin) 
     return self.loginData; 

    return self.profileData; 
} 

现在您只需1个的UITableViewController实例,但一个BOOL告诉你使用哪个数据源。

0

哇,这太奇怪了。

我最后一次做这样的事情,我只是用多个视图,隐藏一个显示当分段控制被窃听另一个。还有其他可能的解决方案,但这可能是最简单的,也许是最有效的。

+0

呀,我是这样做的,但是你不能通过statusBar向上滚动,并且在推送视图时遇到问题。 – runmad 2010-01-14 19:00:11

2

表视图缓存电池内部它采用了显示数据。所以,如果你改变了数据源,你也应该检查你的是 - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath方法,所有的细胞更新到正确的新值。

从你的描述它听起来就像是使用缓存的UITableViewCell实例,而不是将其升级到在所有情况下正确的新数据。或许,这样的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.frame = CGRectZero; 
     cell.textLabel.font = //Set font; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.text = @"My Text for this cell"; // <==== Not good! Move it out of this if block 
    } 
    // Set cell text here 
} 

我发现这种问题的最简单的解决方案是只是让你使用根据数据源的细胞创建(CellIdentifier)的字符串。在这种情况下,您不要混合不同内容类型的单元格(如果单元格根据模式需要有不同的外观,这也可以帮助您)。

9

我有完全相同的问题。我的解决办法是让隐藏的tableView,改变它的来源,重新加载数据,并重新做的tableView可见。在C#(MonoTouch的)

例子:

tableView.Hidden = true; 
tableView.Source = newTableViewSource; 
tableView.ReloadData(); 
tableView.Hidden = false; 
+0

非常感谢您的博文,我一直在努力改变数据源数月! – TChadwick 2012-11-21 15:26:31

0

我有同样的问题,你需要做的是使用内- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不同的小区标识的内容。

if (self.segmentedControl.selectedSegmentIndex == 0) { 
    self.cellIdentifier = @"segmentOne"; 
} else { 
    caseAtIndexPath = [self.awaitingReviewCaseList caseAtIndex:indexPath.row]; 
    self.cellIdentifier = @"segmentTwo"; 
} 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath]; 
1

我有这个问题,原来是因为我的CellIdentifiers都是一样的...

static NSString *CellIdentifier = @"Cell"; 

更改其中的一个细胞布局中正确

static NSString *CellIdentifier2 = @"Cell2";