Display two array values in single tableview cell

前端 未结 4 992
孤街浪徒
孤街浪徒 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: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;  
     }
    

提交回复
热议问题