2 label in UITableViewCell without customizing cell - ios

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

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

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

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

Related

'UITableViewCell' not showing label

This is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
self.credits = (UILabel*)[cell viewWithTag:101];
self.credits.text = #"Available Credits";
cell.backgroundColor = [UIColor redColor];
self.credits.textColor = [UIColor blackColor];
[cell.contentView addSubview:self.credits];
credits is the label here
You store a cell label in your UIViewController and then try to addSubview in the content view.
The right approach for this should be:
Create a subclass of UITableViewCell
Assign a label inside this cell as a property (could be by code, by xib)
In cellForRowAtIndexPath: use that property name to assign Available Credits
Your cellForRowAtIndexPath should look like this in the end:
- (MyCustomUITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myNewProperty.text = #"Available Credits";
}

in didselect row my first row returnig label text instead of my custom label text

here i have a table view where i'm adding custom label which stores some
quiz ids from array
where cell.textlabel.text stores quiz names from array
problem:
when i'm selecting first row in didselectRowAtIndexPath
it returns cell.textlabel.tex but for second row it is returning correct value that is
quiz ids
here is code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [quizname objectAtIndex:indexPath.row];
UILabel *lbl = [[UILabel alloc]init];
lbl.tag=indexPath.row;
[cell addSubview:lbl];
lbl.text = [quizIds objectAtIndex:indexPath.row];
return cell;
}
in did selectrow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = (UILabel *) [cell viewWithTag:indexPath.row];
NSString *qid =label.text;
NSLog(#"%#",qid);
}
i'm getting quid is equal to cell.texlabel.text for firsr row but not in others rows
they are getting quiz ids perfectly
any help most valueble for me
Try using customTableViewCell & add your label inside the customCell. Adding UILabel inside cellForRowAtIndexPath is not a good approach.

CustomCell without customCell class

this is my problem. I would to create a custom cell without create a custom cell class.
I know that this is possible. I've created in storyboard a tableviewcontroller with a prototype cell with a label. In the attibutes I've set the cell name "mycell". And the code that I used is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier = #"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
}
But when my app run I see only a empty table without a cell with my label.
Your UILabel title is nil. First create a UILabel and add as a subview to your cell. You can do this like below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *title = (UILabel*) [cell viewWithTag:1];
if (title == nil) {
title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 15)];
title.tag = 1;
[cell.contentView addSubview:title];
}
title.text = #"Hi";
return cell;
}

align part of text left and part right in Xcode

I am filling a UITableView table with nutrition facts. Each row includes the absolute amount of the nutrient as well as the percent daily value. I would like to align the amount to the left side of each row and the percent daily value to the right side so that the information looks neater and so that all of the values line up. Is there any way I can do this? Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"NutritionCell" forIndexPath:indexPath];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"NutritionCell"];
}
CGRect oldFrame = cell.frame;
cell.textLabel.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height);
cell.detailTextLabel.frame = CGRectMake(oldFrame.origin.x + tableView.frame.size.width/2, oldFrame.origin.y, tableView.frame.size.width/2, oldFrame.size.height);
cell.textLabel.text = [factamount objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [percentDV objectAtIndex:indexPath.row];
return cell;
}
You can also do this with UITableViewCellStyleValue1. Which automatically adds 2 labels to the cell:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"CELLID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
}
cell.textLabel.text = #"AMOUNT TEXT";
cell.detailTextLabel.text = #"PERCENT TEXT";
return cell;
}
You can use the following code,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Left";
cell.detailTextLabel.text = #"Right";
return cell;
}
If you want to use multiple custom labels in cells(more than two), you can do that as well and add it as subview of cell.contentView and align using textAlignment property. You can set the frame for these labels to display in appropriate places.
In that case you need to do it as
myLabel1.textAlignment = NSTextAlignmentLeft;
myLabel2.textAlignment = NSTextAlignmentRight;

Table view with custom cell (programmatically)

So far, I used to create custom nibs to make my cell as I wanted but this time, the height of a cell will change from one to another so that I can't create a fixed-size cell's nib.
So I decided to create it programmatically ... Is the way below the good way to achieve it ?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
[pseudoAndDate setTag:1];
[cell addSubview:pseudoAndDate];
[pseudoAndDate release];
}
CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];
UILabel *label = (UILabel *)[cell viewWithTag:1];
[label setText:[NSString stringWithFormat:#"%# | %#",thisRecord.author,thisRecord.date]];
return cell;
}
or .. am i missing something here ? Cause so far it doesn't seem to work ;)
Thanks,
Gotye.
Why create a label when you don't need to? Use the UITableViewCell's label.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%# | %#",thisRecord.author,thisRecord.date];
return cell;
}
If your problem is that the height varies from cell to cell you can use the method:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
From UITableViewDelagate to achieve it
New link for custom UITableViewCell programmatically Apple Documentation UITableViewCell

Resources