2013-07-29 4 views
1

Вопрос об изменении стиля ячейки. В раскадровке, я создал мобильный: содержание: динамические прототипыИзменение стиля ячейки uiTableview для раздела

в коде, число секций: 2.

Раздел можно использовать макет, созданный в раскадровке. Ячейки заполняются с данными из базы данных. Я пытаюсь изменить макет для ячеек в разделе 2 в стиле value1.

Как это можно сделать?

Часть кода:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cartCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    switch (indexPath.section) { 

     case 0: 
     {  
      // Configure the cell... 
      NSInteger row = [indexPath row]; 
      [[cell textLabel] setText:[cartItems objectAtIndex:row]]; 

      return cell; 
      break; 
     } 

     case 1: 
     { 


      NSInteger row = [indexPath row]; 
      switch (row) { 
       case 0: 
        [[cell textLabel] setText:@"Total1"]; 
        break; 
       case 1: 
        [[cell textLabel] setText:@"Total2"]; 
      } 

      return cell; 
      break; 
     } 
    } 
} 
+0

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

+0

Посмотрите ответы. дайте мне немного времени, это побочный проект. – TomVD

ответ

2

Способ сделать это, чтобы создать 2 разных динамических прототипа в раскадровке, каждый со своим собственным идентификатором. Тогда в cellForRowAtIndexPath, просто из очереди ячейки с идентификатором вы хотите внутри заявление переключателя или иначе, если п:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.section == 0) { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
     cell.textLabel.text = self.theData[indexPath.section][indexPath.row]; 
     return cell; 

    }else{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell_value1" forIndexPath:indexPath]; 
     cell.textLabel.text = self.theData[indexPath.section][indexPath.row]; 
     cell.detailTextLabel.text = @"detail"; 
     return cell; 

    } 
} 
+0

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

0

Вы должны установить стиль ячейки для ячейки Попробуйте ниже код

There are four inbuilt styles supported in iOS 
    1. UITableViewCellStyleDefault 
    2. UITableViewCellStyleValue1 
    3. UITableViewCellStyleValue2 
    4. UITableViewCellStyleSubtitle 

Try, чтобы играть с ними и получить желаемый результат

static NSString * reuseIdentifer = @ "ReuseIdentifier";

// Try to reuse the cell from table view 
     UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:reuseIdentifer]; 

     // If there are no reusable cells; create new one 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 


             reuseIdentifier:reuseIdentifer]; 
      } 
cell.textLabel.tex = @"Your desired text"; 
return cell; 
Смежные вопросы