当我在Ios中滚动表格列表时,正在加载重复数据

时间:2015-09-22 05:53:18

标签: ios objective-c

你好iOS和我的项目我已经使用自动布局以编程方式添加了一个表列表,所以一切都很好。

但是当我滚动表格列表时,每个单元格中都会加载重复的内容。

我在这做错了什么?(我的意思是第一个细胞数据在所有细胞中加载)

我的代码: -

- (void)viewDidLoad {
    [super viewDidLoad];

    tableList = [[UITableView alloc]init];
    tableList.translatesAutoresizingMaskIntoConstraints = NO;
    tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableList.dataSource=self;
    tableList.delegate=self;
    tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"];
    [self.view addSubview:tableList];

    NSDictionary * views = NSDictionaryOfVariableBindings(tableList);

    NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableList]-0-|" options:0 metrics:nil views:views];

    NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableList]-0-|"options:0 metrics:nil views:views];

    [self.view addConstraints:horizentalConstraint];
    [self.view addConstraints:verticalConstraint];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {

        return 365;
    }
    else if(indexPath.row == 1)
    {
    return 365;
    }
    else
    {
    return 50;
    }
}


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

    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (Cell == nil) {

        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (indexPath.row == 0) {
    //Adding Doctor personal details on table view cell
    Personimage = [[UIImageView alloc]init];
    Personimage.image = [UIImage imageNamed:@"ram.jpeg"];
    Personimage.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:Personimage];

    PersonName = [[UILabel alloc] init];
    PersonName.text = @"Mr.Ramakrishna";
    PersonName.font = [UIFont fontWithName:@"Bitter-Regular" size:14.0];
    PersonName.textColor = [UIColor blackColor];
    PersonName.translatesAutoresizingMaskIntoConstraints = NO;
    PersonName.lineBreakMode = NSLineBreakByWordWrapping;
    PersonName.numberOfLines = 0;
    [Cell.contentView addSubview:PersonName];

    Profession = [[UILabel alloc] init];
    Profession.text = @"MD (Medician)";
    Profession.font = [UIFont fontWithName:@"Bitter-Regular" size:14.0];
    Profession.textColor = [UIColor blackColor];
    Profession.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:Profession];

    Experience = [[UILabel alloc] init];
    Experience.text = @"Experience:12 Years";
    Experience.font = [UIFont fontWithName:@"Bitter-Regular" size:14.0];
    Experience.textColor = [UIColor blackColor];
    Experience.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:Experience];

    Adress = [[UILabel alloc] init];
    Adress.font = [UIFont fontWithName:@"Bitter-Regular" size:14.0];
    Adress.text = @"FreeDom Clinic,425 Glower Complex,NY 4582 ";
    Adress.textColor = [UIColor blackColor];
    Adress.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:Adress];

    mapView = [[MKMapView alloc] init];
    mapView.translatesAutoresizingMaskIntoConstraints = NO;
    [Cell.contentView addSubview:mapView];


NSDictionary * viewsDic = NSDictionaryOfVariableBindings(Personimage,PersonName,Profession,Experience,Adress,bottomView,VirtualTretementButton1,VirtualTretementButton2,SendMessage1,SendMessage2,LikeButton,RefreshButton,star1,star2,star3,star4,star5,mapView);

    //Applying autolayouts for person details
    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Personimage(80)]-5-[PersonName(200)]"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];

    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Personimage(80)]-5-[Profession(200)]"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];

    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Personimage(80)]-5-[Experience(220)]"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];

    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Adress]-10-|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];

    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[Personimage(80)]"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];

    [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[PersonName(30)]-(-1)-[Profession(30)]-(-1)-[Experience(30)]-[Adress(30)]"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:viewsDic]];


 //Applying autolayouts for mapkit

        [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[mapView]-10-|"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDic]];

        [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[Adress]-10-[mapView(150)]"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDic]];
}

 if (indexPath.row == 1) {


    }

   } 

2 个答案:

答案 0 :(得分:0)

您可以在运行时创建UILabel,UIImageview和MKMapView,并在滚动和创建期间创建每次创建并添加到Cell.contentView

所以如果条件允许,请将其放在下面。希望能为您提供全面的帮助

if (Cell == nil) {

        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

答案 1 :(得分:-1)

之后

WebClient

添加:

if (Cell == nil) {
    Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

或者为您添加到contentView的每个子视图分配一个标记,并在创建和再次添加之前检查contentView中是否存在具有该标记的视图。

例如:

for (UIView *subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
}