标签:c style class blog code java
UITableViewCell重用
第一种解决方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *subViews = cell.contentView.subviews; for (UIView *view in subViews) { [view removeFromSuperview]; } }
以上只是列举了方法实现的位置,并没有将所有代码写出来。上面的实现方法是将cell.contentView上面的子视图全部取出来,把它们一一移除,这是解决问题的一种方法,如果子视图过多的话,每次重用的时候都会一一把子视图移除会在程序的执行效率上产生问题。
第二种解决办法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dicUserInfo = [self.statusList[indexPath.section] objectForKey:@"user"]; NSDictionary *statusInfo = self.statusList[indexPath.section]; NSUInteger widthSpace = 5; //cell的重用队列 static NSString *cellIdentifier = @"statusCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; //下面是对微博头部的定义 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 35, 35)]; imageView.tag = 1000; [cell.contentView addSubview:imageView]; UILabel *labelScreenName = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+widthSpace, 2, 100, 20)]; labelScreenName.font = [UIFont systemFontOfSize:12.0f]; labelScreenName.tag = 1001; [cell.contentView addSubview:labelScreenName]; UILabel *labelCreateTime = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+widthSpace,CGRectGetHeight(labelScreenName.frame)+ 2,150,20)]; labelCreateTime.font = [UIFont systemFontOfSize:12.0f]; labelCreateTime.tag = 1002; [cell.contentView addSubview:labelCreateTime]; UILabel *labelSource = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(labelCreateTime.frame)+widthSpace, CGRectGetHeight(labelScreenName.frame)+2, 100, 20)]; labelSource.tag = 1003; labelSource.font = [UIFont systemFontOfSize:12.0f]; [cell.contentView addSubview:labelSource]; } UIImageView *imgView = (UIImageView*)[cell.contentView viewWithTag:1000]; NSURL *imgURL = [NSURL URLWithString:[dicUserInfo objectForKey:@"profile_image_url"]]; [imgView setImageWithURL:imgURL]; //微博用户名称 UILabel *screenName = (UILabel*)[cell.contentView viewWithTag:1001]; screenName.text = [dicUserInfo objectForKey:@"screen_name"]; //微博创建时间 UILabel *createTime = (UILabel*)[cell.contentView viewWithTag:1002]; NSString *strDate = [statusInfo objectForKey:@"created_at"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"]; NSDate *dateFromString = [dateFormatter dateFromString:strDate]; NSTimeInterval interval = [dateFromString timeIntervalSinceNow]; createTime.text = [NSString stringWithFormat:@"%d分钟之前",abs((int)interval/60)]; //微博来源 UILabel *labelSource =(UILabel*)[cell.contentView viewWithTag:1003]; NSString *xmlSourceString = [statusInfo objectForKey:@"source"]; NSDictionary *dicSource = [NSDictionary dictionaryWithXMLString:xmlSourceString]; labelSource.text = [dicSource objectForKey:@"__text"]; return cell; }
第三种解决办法
iOS中UITableViewCell的重用问题解决方案,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/xinianhao/p/3776219.html