【问题标题】:UITableView data not getting flushed and unable to reload new dataUITableView 数据未刷新且无法重新加载新数据
【发布时间】:2012-01-19 00:59:31
【问题描述】:

我正在填充表格,该表格包含动态单击按钮时每行中的 4 个标签。

当我尝试将不同的数据动态插入到同一个表中时,它不会刷新旧数据,因此旧数据会与新数据重叠。

我需要做哪些改变?

*.m

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView==myTableView){

        return [tableData count];
    }
    else if(tableView==aTableView){
        return [a count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView==myTableView){
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12.0];
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    return cell;
}
    else if(tableView==aTableView){

        static NSString *CellIdentifier=@"CellIdentifier"; 

        static NSInteger StateTag = 1;
        static NSInteger CapitalTag = 2;
        static NSInteger StateTag1 = 3;
        static NSInteger StateTag2 = 4;


        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){

                cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
                CGRect frame;
                frame.origin.x = 10; 
                frame.origin.y = 5;
                frame.size.height = 35;
                frame.size.width = 80;


                UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
                capitalLabel.tag = CapitalTag;
                [cell.contentView addSubview:capitalLabel];

                frame.origin.x += 135;
                UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
                stateLabel.tag = StateTag;
                [cell.contentView addSubview:stateLabel];

                frame.origin.x += 100;
                UILabel *stateLabel1 = [[UILabel alloc] initWithFrame:frame];
                stateLabel1.tag = StateTag1;
                [cell.contentView addSubview:stateLabel1];

                frame.origin.x += 100;
                UILabel *stateLabel2 = [[UILabel alloc] initWithFrame:frame];
                stateLabel2.tag = StateTag2;
                [cell.contentView addSubview:stateLabel2];

                capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
                stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];
                stateLabel1 = (UILabel *) [cell.contentView viewWithTag:StateTag1];
                stateLabel2 = (UILabel *) [cell.contentView viewWithTag:StateTag2];

                capitalLabel.text=[a objectAtIndex:indexPath.row];
                stateLabel.text = [b objectAtIndex:indexPath.row];
                stateLabel1.text = [c objectAtIndex:indexPath.row];
                stateLabel2.text = [d objectAtIndex:indexPath.row];
            }            
            return cell;
        }
}


-(IBAction)btnClicked:(id)sender{
    NSMutableDictionary *cells = (NSMutableDictionary*)[self.aTableView valueForKey:@"_reusableTableCells"];
    [cells removeAllObjects];

    [self gainer];
}

-(IBAction)btnClicked1:(id)sender {

    [self looser];
}

-(void)gainer{   

    arr1=[[NSMutableArray alloc]init];
    arr2=[[NSMutableArray alloc]init];
    a=[[NSMutableArray alloc]init];
    b=[[NSMutableArray alloc]init];
    c=[[NSMutableArray alloc]init];
    d=[[NSMutableArray alloc]init];

    [a removeAllObjects];
    [b removeAllObjects];
    [c removeAllObjects];
    [d removeAllObjects];

    //POPULATING OF ARRAYS(a,b,c,d) IS DONE HERE. NOW using ReloadData to populate table.


    [self.aTableView reloadData];
}

-(void)looser{
    //SIMILAR LOGIC AS DEFINED IN gainer() method
}



- (void)viewDidLoad {

    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(200, 31, 310,170)];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];

    //code for fetch
    arr1=[[NSMutableArray alloc]init];
    arr2=[[NSMutableArray alloc]init];
    a=[[NSMutableArray alloc]init];
    b=[[NSMutableArray alloc]init];
    c=[[NSMutableArray alloc]init];
    d=[[NSMutableArray alloc]init];

    aTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame] style:UITableViewStyleGrouped];
    aTableView.dataSource = self;
    aTableView.delegate = self;
    aTableView.frame = CGRectMake(100, 150, 200, 300);
    aTableView.tag=1;
    [self.view addSubview:aTableView];

    [super viewDidLoad];
}

有什么建议吗?

【问题讨论】:

    标签: iphone uitableview label ipod viewcontroller


    【解决方案1】:

    使用 XIB 创建自定义单元格。这将大大简化您的代码,实际上可能会加快它,因为您没有进行标记查找。为需要设置的标签创建直接属性。如果标签需要为空,请将nil 传递给标签的text 属性。

    另一个提示是不要在tableview:cellForRowAtIndexPath: 中进行子视图定位,这可能会产生不可预知的结果,应改为使用tableView:willDisplayCell:forRowAtIndexPath:

    顺便说一句,您分配但未发布的所有标签都有多个泄漏。

    【讨论】:

    • 我需要在哪里设置niltext label 的属性,我在哪里release 他们? tableView:willDisplayCell:forRowAtIndexPath: 给出 SIGABRT 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2011-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多