TableView with multiple prototype cells

前端 未结 3 1944
广开言路
广开言路 2021-02-01 07:47

I had a simple question regarding a table view with 3 different kinds of prototype cells. The first two occur just once while the third occurs 4 times. Now what I\'m confused ab

相关标签:
3条回答
  • 2021-02-01 07:58

    Here i wrote code like:

    #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;
     }
    
    0 讨论(0)
  • 2021-02-01 08:02

    If you are using three prototype then use three identifiers. Because only one identifier will cause problem. And you will get wrong result. So code like this.

    if(indexPath.row==0){
     // Create first cell
    }
    
    if(indexPath.row==1){
     // Create second cell
    }
    
    else{
     // Create all others
    }
    

    You can use switch case also here for best performance.

    0 讨论(0)
  • 2021-02-01 08:23
    - (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]; 
    }
    
    0 讨论(0)
提交回复
热议问题