UITableView's cellForRowAtIndexPath look like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell Identifier";
cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UILabel *likelabelnew = [[UILabel alloc]init];
likelabelnew.frame = CGRectMake(230,5,50,20);
likelabelnew.text = #"old text";
likelabelnew.tag=indexPath.row;
[cell addSubview:likelabelnew];
}
return cell;
}
Add this code whereever you want to update your cell text. Suppose you have to update UILabel with tag 100 . Then add one more check :
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *subviews = cell.subviews;
for (UIView *subview in subviews) {
if ([subview isKindOfClass:[UILabel class]] && subview.tag == 100) {
UILabel *mylabel = (UILabel *)subview;
//Update your lable here
}
}
Try this.
First of all you don't need to add custom UILabel in the function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then do this where you want to update your label.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = cell.textLabel;
label.frame = CGRectMake(10,10,230,31);
label.text = #"Your text";
Related
I created UITableView inside my UIViewController and I built a prototype cell in the storyboard. I assigned tags for all the elements in the cell I want to change and I am changing them in the code.
When the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called, my data is processed and I can see a cell appear with the red background colour I set in storyboard, however the images or text is missing. I think the problem is with the implementation but I can't figure out what I am doing wrong.
This is what the storyboard looks like:
This is the relevant code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if(tableView == youLikeTableView)
{
UIView* youLikeImageFrame = (UIView *)[cell.contentView viewWithTag:100];
UIImageView* youLikeImageView = (UIImageView *)[cell.contentView viewWithTag:101];
UILabel *youLikeNameLabel = (UILabel *)[cell.contentView viewWithTag:102];
for(int i = 0; i < [youLike count]; i++)
{
youLikeImageFrame.layer.cornerRadius = youLikeImageFrame.frame.size.width / 2;
[youLikeImageView setImage:[youLikeImages objectAtIndex:i]];
youLikeImageView.layer.cornerRadius = youLikeImageView.frame.size.width / 2;
youLikeNameLabel.text = [youLikeName objectAtIndex:i];
}
}
return cell;
}
And this is the cell appearing without the text or images
you forgot to add them as subview
[cell addSubView:youLikeImageFrame];
[cell addSubView: youLikeImageView]; and
[cell addSubView: youLikeNameLabel];
just before
return 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;
}
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
I am using this method to add an UILabel to my UITableView and its working fine, but I also need to create a method for selecting the row and save into an temp String.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
[cell.contentView addSubview:label];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:#"Whitney-Light" size:20.0];
return cell;
}
but its not working. It was working fine with when I was using cell.textLabel but not with the custom label.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
UILabel *lbltemp = cell1.textLabel;
_parent.labelone.text = lbltemp.text;
}
You could create your own UITableViewCell subclass to allow you to get the reference to your label (and prevent you from creating multiple labels on the cell). Or you could use tags like this:
- (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];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
label.tag = 123123;
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell viewWithTag:123123];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:#"Whitney-Light" size:20.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:123123];
_parent.labelone.text = label.text;
}
Your original code was adding a new label but then trying to get the text from the default label... It was also always creating a new cell and adding a new label to it which is quite wasteful.
Also, you shouldn't usually store text in the label, you should really to to your tableArr to get the text. The tag approach is more suited to updating the label when you reuse the cell or if you're letting the user edit the text (in a UITextField).
The reason it's not working is that you're not referencing the custom label in didSelectRowAtIndexPath. The easiest way to get a reference to the custom label is using tags:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
// Also, this is the proper way to use reuseIdentifier (your code is incorrect)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
[cell.contentView addSubview:label];
label.text = [tableArr objectAtIndex:indexPath.row];
label.font = [UIFont fontWithName:#"Whitney-Light" size:20.0];
label.tag = 1;
return cell;
}
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
UILabel *customLabel = [cell viewWithTag:1];
_parent.labelone.text = customLabel.text;
}
The problem is cell1.textLabel is not the label you created. It's simply the default Label created by the cell.
The solution could be:
Add a tag to the your label when you create your custom cell.
Say label.tag=100;
In didSelectRow,
Use
UILabel *lbltemp=(UILabel *)[cell1.contentView viewWithTag:100];
then _parent.labelone.text=lbltemp.text; should grab your text from that label.
Here ([cell.contentView addSubview:label];_) you are creating your custom label and adding it as subview to cell's content view but in UILabel *lbltemp = cell1.textLabel; you are trying to access the default label of tableview cell .Add some identifier(like tag) to your label and access it in didSelectRowAtIndexPath .
I think you can use default label and refuse from this workaround
just add
cell.textLabel.frame = CGRectMake(10,10, 300, 30);
Also it's better for memory to reuse cells as in Wain's answer
use cell.indentationallevel or cell.indentationwidth
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;
}
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.
In the tableView methods I've used:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5; }
Thank you.
If you are adding UILabel programmatically it needs to be alloc init, should be given a frame and added to the cell as sub view.
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
Should be
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 50, 20)];
title.text = #"Hi";
[cell addSubview:title];
return cell;