Display two array values in single tableview cell

前端 未结 4 991
孤街浪徒
孤街浪徒 2021-01-28 23:58

I want to display webservies array values in tableview for each cell i need to display two values Ex:total ten values mean each cell display 2 values in each row. webservies t

相关标签:
4条回答
  • 2021-01-29 00:25

    In cellForRow: you should send the cell the values from the array at [yourArray objectAtIndex:(indexPath.row * 2)] and [yourArray objectAtIndex:(indexPath.row * 2 + 1)]. so row 6 will get the objects for indexes 12 and 13. Also you should always check if the objects exists. something like - if (yourArray.count > (indexPath.row * 2)) and if (yourArray.count > (indexPath.row * 2 + 1)) than send to cell else don't. (than you will get one object in cell when you get odd number of values.

    0 讨论(0)
  • 2021-01-29 00:28

    Create a custom class of UITableViewCell say CustomCell and add 2 labels to it both occupying half of the space or as per your design what you need. Now say that they are labelOne and labelTwo. From your Controller class you got the array of objects that you need to display in lables. In UITableViewDataSource method use this code

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
          return (dataArray.count+1)/2;  //This will provide correct row count for odd data set, such as when count is 9
    }
    

    and use this code to populate cell label text

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
          CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if(cell==nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];
        }
          cell.lableOne.text = [dataArray objectAtIndex:indexPath.row*2]; 
          if((indexPath.row*2)+1 < dataArray.count){ 
             cell.lableTwo.text = [dataArray objectAtIndex:(indexPath.row*2)+1];
          }
    
          return cell;  
     }
    
    0 讨论(0)
  • 2021-01-29 00:30

    Using % fetch the number of rows for the table view. Then for the last cell if value is null then display blank... you can have a basic logic for that. Take 2 lbls in each cell and display on those lables which contains value in it.

    0 讨论(0)
  • 2021-01-29 00:35

    Follow this tutorial for custom cell and design it the way you want:

    Crafting Custom UITableView Cells

    This is the method where you can set the values for the custom cell labels from each array:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        return cell;
    }
    
    0 讨论(0)
提交回复
热议问题