Uitableview重用细胞问题

时间:2013-12-05 11:59:51

标签: iphone uitableview

我在UITableView的{​​{1}}执行添加操作,因为我滚动了tableview,忽略了第一个UITableViewCell中的值,虽然得到了错误的结果,但我怎么能保持这个值在第一个UITableViewCell中添加最后UITableViewCell

中的值

1 个答案:

答案 0 :(得分:0)

对于UITableViewCell重用问题,如果要以编程方式在UITableViewCell中添加UILabel,UIImageView,UITextField等,请遵循下面给出的代码。

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

UIImageView *img_view;
UILabel *name_lbl;
UILabel *date_lbl;
UIImageView *status_imageview;

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    img_view=[[UIImageView alloc] initWithFrame:CGRectMake(OFFSET,OFFSET,IMAGEWIDTH,50)];
     // Set Image view Properties
    img_view.tag = 101;
    [cell.contentView addSubview:pimg_view];


    name_lbl= [[UILabel alloc] initWithFrame:CGRectMake(5*OFFSET+IMAGEWIDTH,5,200,25)];
   //Set UILabel properties
    pname_lbl.tag = 202;
    [cell addSubview:name_lbl];

    date_lbl= [[UILabel alloc] initWithFrame:CGRectMake(5*OFFSET+IMAGEWIDTH,30,200,25)];
    date_lbl.tag = 303;
    [cell addSubview:date_lbl];

 }else{

     img_view = (UIImageView *)[(cell)viewWithTag:101];
     name_lbl = (UILabel *)[(cell)viewWithTag:202];
     date_lbl = (UILabel *)[(cell)viewWithTag:303];
}


name_lbl.text = @"Set the Value accordingly";
date_lbl.text =  @"Set the Value accordingly";
[img_view setImage:@"Set the image accordingly"];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;
}