2016-01-10 2 views
0

Я столкнулся с проблемой в пользовательской ячейке, прокручивая вверх и вниз значения в смене ячейки. Я добавил две кнопки в последнем разделе. Пожалуйста, проверьте приведенный ниже код. Любая помощь, очень ценится. Благодаря тонну.UITableView Пользовательские значения изменения ячейки iOS 9

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

    DrinkCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (!cell) { 

     cell = [[DrinkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    if (indexPath.section == [tableView numberOfSections] - 1) 
    { 
     cell.tileImage.hidden = YES; 
     cell.pizzaTopins.hidden = YES; 
     // cell.accessoryType = UITableViewCellAccessoryNone; 
     if (indexPath.row == 0) { 

      cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width); 
      UILabel *calorieLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, cell.contentView.frame.size.height/2-30/2, self.tableView.frame.size.width, 30)]; 
      calorieLabel.text = @"1000 Calories | 700 grams of fat"; 
      cell.backgroundColor = backgroundColorApp; 

      [cell addSubview:calorieLabel]; 
     } 
     else if (indexPath.row == 1) { 

      cell.backgroundColor = backgroundColorApp; 
      NSLog(@"cell width : %f",cell.contentView.frame.size.width); 

      UIView *quantityView = [[UIView alloc]init]; 
      quantityView.frame = CGRectMake(25 ,cell.contentView.frame.size.height/2-50/2, cell.contentView.frame.size.width-55, 50); 
      quantityView.backgroundColor = [UIColor whiteColor]; 
      quantityView.clipsToBounds  = YES; 
      quantityView.userInteractionEnabled = YES; 
      quantityView.layer.cornerRadius = 10.0f; 

      UIButton *decreaseButton = [[UIButton alloc]initWithFrame:CGRectMake(10, quantityView.frame.size.height/2-35/2, 35, 35)]; 
      [decreaseButton setTitle:@"-" forState:UIControlStateNormal]; 
      [quantityView addSubview:decreaseButton]; 
      [decreaseButton addTarget:self action:@selector(decreaseQuantity:) forControlEvents:UIControlEventTouchUpInside]; 
      [decreaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal]; 

      [decreaseButton setTintColor:[UIColor greenColor]]; 
      decreaseButton.clipsToBounds = YES; 
      decreaseButton.layer.cornerRadius = 17.5f; 
      decreaseButton.layer.borderColor = quantityButtonColor.CGColor; 
      decreaseButton.layer.borderWidth = 1.5f; 
      int i =0; 
      quantityLabel = [[UILabel alloc]initWithFrame:CGRectMake(quantityView.frame.size.width/2-30/2, quantityView.frame.size.height/2-30/2, 30, 30)]; 
      quantityLabel.text = [NSString stringWithFormat:@"%d",i]; 
      [quantityLabel setFont:[UIFont boldSystemFontOfSize:18]]; 
      quantityLabel.textColor = quantityButtonColor; 
      [quantityView addSubview:quantityLabel]; 


      UIButton *increaseButton = [[UIButton alloc]initWithFrame:CGRectMake(quantityView.frame.size.width-45, quantityView.frame.size.height/2-35/2, 35, 35)]; 

      [increaseButton setTitle:@"+" forState:UIControlStateNormal]; 
      [increaseButton addTarget:self action:@selector(increaseQuantity:) forControlEvents:UIControlEventTouchUpInside]; 
      [quantityView addSubview:increaseButton]; 
      increaseButton.clipsToBounds = YES; 
      [increaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal]; 

      increaseButton.layer.cornerRadius = 17.5f; 
      increaseButton.layer.borderColor = quantityButtonColor.CGColor; 
      increaseButton.layer.borderWidth = 1.5f; 

      [cell addSubview:quantityView]; 
      [quantityView addSubview:decreaseButton]; 
      [quantityView addSubview:increaseButton]; 
     } 
     else if (indexPath.row == 2) { 
      addTocartButton = [[UIButton alloc]initWithFrame:CGRectMake(25, cell.contentView.frame.size.height/2 - 50/2, cell.contentView.frame.size.width-55, 50)]; 

      [addTocartButton setTitle:@"Add to Cart" forState:UIControlStateNormal]; 
      [addTocartButton addTarget:self action:@selector(addToCartOnCreatePizza:) forControlEvents:UIControlEventTouchUpInside]; 
      addTocartButton.backgroundColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0]; 
      // [cell.contentView addSubview:addTocartButton]; 
      [cell.contentView insertSubview:addTocartButton atIndex:0]; 
      addTocartButton.clipsToBounds = YES; 
      addTocartButton.layer.cornerRadius =10.0f; 

     } 
    } 

    cell.pizzaTopins.textColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0]; 
    cell.pizzaTopins.text = [dataArray objectAtIndex:indexPath.row]; 
    cell.tileImage.image = [UIImage imageNamed:@"profile44.png"]; 

    // cell.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 

    return cell; 
} 
+0

Что именно вы стоите перед лицом? не получая ваш вопрос. – Ryuk

+0

в то время как прокрутка, tableview выглядит так грязно. Кнопки и метки сбрасываются. –

+0

Нет проблем, если я удалю кнопки, которые я добавил в последнем разделе. Но если я добавляю кнопки, прокручивая и поднимаясь, кнопки входят в первую строку. –

ответ

0

dequeueReusableCellWithIdentifier будет «повторно» ранее созданные клетки, так что вы должны убедиться, чтобы повторно инициализировать все добавленные вручную/измененные атрибуты, включая любые кнопки добавленных программно.

Снимки экрана помогут нам правильно диагностировать проблему, но ваш код уязвим для затяжных атрибутов повторно используемых ячеек, поэтому я бы предложил начать с этого.

Кстати, вы, вероятно, закончите с более простым кодом, если бы определили несколько прототипов ячеек в IB и изменили cellIdentifier на основе строки indexPath.

+0

Спасибо за ваш ответ. Я уже исправил создание другой ячейки клиента в раскадровке. –

Смежные вопросы