菜鸟关于uitableviewcell的问题

iOS 码拜 9年前 (2015-05-08) 887次浏览 0个评论
 

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中时间刷新显示不正确,不知道为什么,请教高手。
谢谢。

5分
[myTableView reloadData];
5分
做个内容的定时更新,就用楼上的方法。
10分
这是由于cell重用引起的,和数据刷新无关,在addsubview前,要先清除contentview中的label

重用是table为了节省内存,如果内存中有cell就使用,没有才创建,一旦界面一进一出就会出现了,再多看点代码就知道,可以不在这个时候重复添加lebel,自己看了

引用 3 楼 arthurchenjs 的回复:

这是由于cell重用引起的,和数据刷新无关,在addsubview前,要先清除contentview中的label

重用是table为了节省内存,如果内存中有cell就使用,没有才创建,一旦界面一进一出就会出现了,再多看点代码就知道,可以不在这个时候重复添加lebel,自己看了

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

20分
你不是有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];这是遍历全清,自己看着办
    }


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明菜鸟关于uitableviewcell的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!