cellForRowAtIndexPath委托方法中添加并设置了一个uilabel
//Display dateTime
    NSString *groupTitle = [keys objectAtIndex:section];
    if ([groupTitle isEqualToString:@"更早"]) {
        UILabel *dateLabel  = [[UILabel alloc] initWithFrame:CGRectMake( tableView.bounds.size.width-130, cell.detailTextLabel.frame.origin.y, 130, cell.detailTextLabel.frame.size.height)];
        dateLabel.tag = 2000;
        //只截取日期做显示,暂时不显示时间。
        
        NSString *dateText = [pItem getAttribValue:@"datetime"];
        if ([dateText length] == 17) {
            dateText = [dateText substringToIndex:8];
        }
        else if ([dateText length] == 18) {
            dateText = [dateText substringToIndex:9];
        }
        else
        {
             dateText = [dateText substringToIndex:10];
        }
         
        
        dateLabel.text = dateText;
        dateLabel.textAlignment = UITextAlignmentLeft;
        dateLabel.backgroundColor = [UIColor clearColor];
        dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin ;
        
        [cell.contentView addSubview:dateLabel];
        [dateLabel release];
    }时间可以在第一页中正常的显示,若翻页或屏幕上下滑动uilabel中时间刷新显示不正确,不知道为什么,请教高手。
谢谢。

解决方案 »

  1.   

    这是由于cell重用引起的,和数据刷新无关,在addsubview前,要先清除contentview中的label重用是table为了节省内存,如果内存中有cell就使用,没有才创建,一旦界面一进一出就会出现了,再多看点代码就知道,可以不在这个时候重复添加lebel,自己看了
      

  2.   

    麻烦请教一下uilabel要怎么清除?谢谢。
      

  3.   

    你不是有tag = 2000了吗
    viewWithTag可以取到label
    然后label调用removeFromSuperView调查一下cell重用的问题吧例如: 你一个屏幕可以显示5个cell,但你有6个cell总共,因为重用机制系统只会alloc 5个cell, 当你滚动屏幕来显示第六个cell的时候,系统并不会帮你重新在alloc一个cell,而是使用第一个cell,就是由于滚动被移出屏幕外的那个cell来重新作为第“六”个cell。但是由于第一个cell并没有被release掉,所以里面的view还是会保留下来,就是你的textfield,label,image之类的东西都还在,所以又会重复显示一遍。解决方法可以有,每次新建一个cell,这样最直接易懂,就是不用他的重用,每次alloc。或者在你调用 [cell.contentview addSubview:] 之前将contentview 里的subview全部清空。    UITableViewCell *cell = [_filterTableView dequeueReusableCellWithIdentifier:startDateCellIdentifier];
        
        if ( cell == nil ) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:startDateCellIdentifier] autorelease];
        }
        else
        {
             for(uiview *view in cell.contentView.subviews)
                [view removeFromSuperview];这是遍历全清,自己看着办
        }