Ali
Ali

Reputation: 29

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;
}

Upvotes: 3

Views: 1401

Answers (3)

Lee xw
Lee xw

Reputation: 133

you can add this code in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

    cell.detailTextLabel.text = cityObject.cityPopulation;

for detail ,please read https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

Upvotes: 0

Garfield81
Garfield81

Reputation: 521

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;
}

Upvotes: 3

stktrc
stktrc

Reputation: 1639

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];

Upvotes: 1

Related Questions