2012-07-27 37 views
0

我有一个自定义的UITableViewCell一个UITableView。无法刷新可重复使用的UITableViewCell数据

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

//create the cell 
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
cell.label.text = .. 
cell.label2.text = .. 
cell.label3.text = .. 

一切正常,我所有的数据正确加载等

现在,我对这个观点,即打开另一个视图,用户可以选择哪个标签来显示控制器的按钮。所以,举例来说,显示标签1和3,而不是2 ... 然后,点击Done的时候,我想的tableView进行更新,以反映新的选择,但是由于细胞装载一个reuseCellId,没有变化被显示。我如何强制单元格重新创建?

回答

0

我刚好破坏的tableview,每一次重新创建它解决了这个问题。

0

这是不是一个好方法,你可以用,当你想刷新细胞

我不知道是否有这样做的任何其他更好的方式使用不同的标识符做到这一点
的一种方式。

+0

我不认为这是一个很好的方法。 – 2012-07-27 15:47:01

+0

是的,我知道这一点 – kthorat 2012-07-27 16:34:37

0

我的事情,你能做的最好的事情是存储在某种结构(一组与要显示的将是确定这里的标签索引)的细胞结构和改变这种结构与您的按钮,并重新加载表视图。然后,在你的tableView:cellForRowAtIndexPath:方法中,你应该检查那个配置结构,以便知道哪些按钮应该是可见的。

此代码可以帮助:

@interface MyViewController : UIViewController 
{ 
    ... 
    NSMutableSet *_labelsToShow; 
} 

... 
@property (nonatomic, retain) NSMutableSet labelsToShow 

@end 


@implementation MyViewController 
@synthesize labelsToShow = _labelsToShow; 

- (void)dealloc 
{ 
    [_labelsToShow release]; 
    ... 

} 


//you may know which button has to add/remove each label, so this needs to be fixed with your logic 
- (IBAction)myButtonAction:(id)sender 
{ 
    if (hasToShowLabel) 
    { 
     [self.labelsToShow addObject:[NSNumber numberWithInteger:labelIdentifier]]; 
    } else 
    { 
     [self.labelsToShow removeObject:[NSNumber numberWithInteger:labelIdentifier]]; 
    } 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"myCell"; 
    MyCustomCell *cell = (MyCustomCell *)[tableView dequeReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault] autorelease]; 
    } 

    cell.label0.hidden = (![self.labelsToShow containsObject:[NSNumber numberWithInteger:0]]); 
    cell.label1.hidden = (![self.labelsToShow containsObject:[NSNumber numberWithInteger:1]]); 
    cell.label2.hidden = (![self.labelsToShow containsObject:[NSNumber numberWithInteger:2]]); 
    ... 

    return cell; 
} 


@end 

祝你好运与此!