3

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;
}
Xu Yin
  • 3,932
  • 1
  • 26
  • 46
Ali
  • 29
  • 5

3 Answers3

3

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;
}
Garfield81
  • 521
  • 2
  • 9
  • but now the problem is that my PushDetail view is not working because we change the Identify ID, and when I change it to Cell again show just one label instead of two. – Ali Feb 27 '14 at 02:46
  • if you know please let me know what I have to do with that problem! after change the identify ID the pushViewDetail is not working, and when I change it to @Cell is working but again in UITableViewCell just show one label !! – Ali Feb 27 '14 at 05:55
  • Do you mean the CellIdentifier = @"CustomCell" is breaking your pushViewDetail? Does it crash? What error do you get? – Garfield81 Feb 27 '14 at 20:03
  • no it's working perfectly but because I change the CellIdentifier to CustomCell, now when I tap on the row my pushView detail is not working and just selecting the row without going to detail video page. – Ali Feb 27 '14 at 22:24
  • yes, I dont have any error just now when I tap the row I can not going to next page( detailvie) and just select the row and the pushdetailview is not working. – Ali Feb 27 '14 at 22:32
  • Do you have a segue pushing the pushDetailView or are you implementing - (UITableViewCell *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath – Garfield81 Mar 03 '14 at 17:13
  • yes, you see all code here [link]http://stackoverflow.com/questions/22065940/define-the-uitableviewcell-out-of-storyboard-with-nib/22068154?noredirect=1#22068154 – Ali Mar 10 '14 at 03:59
  • when I used your code I have two labels in one row but the pushdetailview is not working and I can not going to other page ! cheers – Ali Mar 10 '14 at 04:01
  • In your storyboard select the view controller with the table cell prototype and in the properties change the cell type to Subtitle and the identifier to customCell. – Garfield81 Mar 10 '14 at 04:55
1

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];
stktrc
  • 1,599
  • 18
  • 30
  • this will call addSubview over and over again. – Lee xw Feb 27 '14 at 02:22
  • You might want to revise that. Using `addSubview` on a UITableViewCell isn't recommended. You should always add custom views to `contentView` so cell transitions are handled properly. – Simon Germain Feb 27 '14 at 02:24
0

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

Lee xw
  • 133
  • 9