Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 label in UITableViewCell without customizing cell

I want to put two text label in one cell. I used the Json to show data from server and I want to show first title then the text below on( not subtitle).

this is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    City * cityObject;
    cityObject = [ citiesArry objectAtIndex:indexPath.row];
    cell.textLabel.text = cityObject.cityState;
    cell.textLabel.text = cityObject.cityPopulation;
}
like image 684
Ali Avatar asked May 03 '26 03:05

Ali


2 Answers

Is the cell designed in a storyboard or NIB? If so you can either make that cell style to be subtitle or add the two labels there to the contentView. Assign them unique tags so you can query them in the cellForRowAtIndexPath: method and set their text.

If you do not have storyboards or NIB designing the cell then something like the following should work.

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

   static NSString *CellIdentifier = @"CustomCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (!cell)
   {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]
   }

   // Configure the cell...

   City * cityObject;
   cityObject = [ citiesArry objectAtIndex:indexPath.row];
   cell.textLabel.text = cityObject.cityState;
   cell.detailTextLabel.text = cityObject.cityPopulation;
}
like image 186
Garfield81 Avatar answered May 05 '26 16:05

Garfield81


Nice and simple for this one..

Just create two labels and add them to the cell view. Eg..

UILabel* label1 = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, 10, 50))];
    label1.text = cithObject.cityState;
    [cell addSubview:label1];

    UILabel* label2 = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, 10, 50))];
    label2.text = cityObject.cityPopulation;
[cell addSubview:label2];
like image 32
stktrc Avatar answered May 05 '26 17:05

stktrc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!