TableView с несколькими ячейками прототипа - программирование
Подтвердить что ты не робот

TableView с несколькими ячейками прототипа

У меня был простой вопрос относительно табличного представления с 3 различными типами прототипов. Первые два происходят один раз, а третий - 4 раза. Теперь я запутался в том, как указать в моей cellforRowatindexpath, какой прототип ячейки использовать для какой строки. Итак, я хочу что-то вроде строки 0, использовать прототип 1, для строки 1, использовать прототип 2, для рядов 3,4,5 и 6 использовать прототип 3. Какой лучший способ сделать это? Я даю каждому прототипу идентификатор, а затем использую dequeueReusableCellWithIdentifier: CellIdentifier? Можете ли вы предоставить пример кода?

EDIT:

Все еще не работает. Это код, который у меня есть на данный момент. (У меня есть только один случай для статута переключателя, потому что я просто хочу проверить и посмотреть, генерируется ли ячейка в первой строке или нет, но в настоящее время вид таблицы пуст)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     switch(indexPath.row)
{          
 case 0: {static NSString *CellIdentifier = @"ACell";
                   UITableViewCell *cell = [tableView
                                           dequeueReusableCellWithIdentifier:@"ACell"];
  if(cell==nil) {
    cell=[[UITableViewCell alloc]
          initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"];

                        }
  return cell;
  break;
    }
  }
}

Acell - это мой идентификатор прототипа ячейки, который я создал. I

4b9b3361

Ответ 1

Если вы используете три прототипа, используйте три идентификатора. Потому что только один идентификатор вызовет проблему. И вы получите неправильный результат. Итак, код такой.

if(indexPath.row==0){
 // Create first cell
}

if(indexPath.row==1){
 // Create second cell
}

else{
 // Create all others
}

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

Ответ 2

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

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell.tag == 0) 
   {
    return array1.count;
   }
   else(cell.tag == 1)
   {
    return array2.count;
   }    
}

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

 NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];

 if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
 {
     cellIdentifier = @"cell";
 }
 else if ([membershipType isEqualToString:@"platinum"])
 {
     cellIdentifier = @"premiumCustomCell";
     cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
 }

 cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 if (!cell) {
     cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
}

Ответ 3

Здесь я написал код вроде:

#pragma mark == Tableview Datasource

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
    case 0:
        nRows = shipData.count;
        break;
    case 1:
        nRows = dataArray1.count;
        break;
    default:
        break;
}
return nRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
    case 0:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        //Load data in this prototype cell
        break;
    case 1:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        //Load data in this prototype cell
        break;
    default:
        break;
}
return cell;
 }