numberOfLines of UILabel doesn't work within cell - iphone

All,
I am very new to iphone programming. In the following code, I want the text to show all of the text within the comment label but right now it is truncating it. numberofLines is not working right either. Right now it is doing this. "My name is Fred and I aint dead..." but I want it to display the full text "My name is Fred and I aint dead yet so let me live" even if it has to be on multiple lines.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [self.allCells objectForKey:[NSNumber numberWithInt:indexPath.row]];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"UserCell2" owner:nil options:nil] lastObject];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self.allCells setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]];
}
GSAsynImageView *imgView = (GSAsynImageView*)[cell viewWithTag:1000];
UILabel *lblTitle = (UILabel*)[cell viewWithTag:1001];
UILabel *lblComment = (UILabel*)[cell viewWithTag:1003];
UILabel *lbltime = (UILabel*)[cell viewWithTag:1004];
//lblComment setLineBreakMode:NSLineBreakByWordWrapping];
//lblComment.numberOfLines = 0;
//lblComment.lineBreakMode = UILineBreakModeCharacterWrap;
if(self.arrComments.count==0)
{
imgView.hidden = YES;
lblTitle.text = nil;
lblComment.text = nil;
lbltime.text = nil;
if(indexPath.row==1)lblTitle.text = #"No comments yet";
}
else
{
imgView.hidden = NO;
NSDictionary *dcUser = [self.arrComments objectAtIndex:indexPath.row];
NSString *strBio = [dcUser objectForKey:#"CommentTxt"];
NSString *strDisplayName = [dcUser objectForKey:#"CommenterDisplayName"];
NSString *imgName = [dcUser objectForKey:#"ImageName"];
NSString *usernamex = [dcUser objectForKey:#"CommenterUserName"];
if([imgName isKindOfClass:[NSString class]])
{
if([imgName rangeOfString:#"facebook"].location!=NSNotFound || [imgName rangeOfString:#"twimg"].location!=NSNotFound)
[imgView loadImageFromPath:imgName];
else
[imgView loadImageFromPath:[NSString stringWithFormat:#"%#images/%c/%#/50x50%#",WEBSERVER,[usernamex characterAtIndex:0],usernamex,imgName]];
}
lblTitle.text = strDisplayName;
lblComment.text = strBio;
lbltime.text = [self getDateTitle:[dcUser objectForKey:#"Date"]];
}
return cell;
}

try this bellow code and add in your cell..
UILabel * lblTitle = [[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(110, 31, 200, 50)];
lblTitle.text = #"your Text ";
lblTitle.lineBreakMode = UILineBreakModeWordWrap;// add this line
lblTitle.numberOfLines = 0;// add this line
lblTitle.font = [UIFont fontWithName:#"Helvetica" size:12];
for more detail see my blog with this post from THIS link

try this...
UILabel * label = [[UILabel alloc]init];
[label setFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
label.text = #" Text to be displayed in label ";
label.lineBreakMode = UILineBreakModeWordWrap ;// Wrap at word boundaries
label.numberOfLines = 0;// this line include multiple lines
[cell addSubview:label];

I think it probably because the size of the string you wanna show on the label exceeds the size of that label

You can use the properties of UILabel to increase the number of lines.
Like UILabel* para = [[UILabel alloc]init];
para.numberoOfLines = 10;
and also try changing para.lineBreakMode

Increase the height of the label and make the number of line to 2 for the particular label.
try this code,
lblComment.numberofLines = 2;
[lblComment setFrame:CGRectMake(100,20,200,70)];

The easiest way to do this is in a storyboard or xib. You can add your label (and anything else you want) to the custom cell you get automatically when you have a table view. Make sure your label has a specific width set, and that it has constraints to the top and bottom of the cell (make sure you set number of lines to 0). If you have these, the label will expand with the height of the cell, which you set in tableView:heightForRowAtIndexPath:.

Related

Failed to increase the height of a UILabel for Multi-line UITable

I am trying to resize the height of UILabel in a Table Cell. INnmy case the table is dynamic and I have customized it using two UILabel. The Table will show exactly 11 items, but two of the cells will need to accommodate multi-line text UILabel. I have selectively increase the height of two cells as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 3 || indexPath.row == 10)
return 100;
else
return 46;
}
But I also need to increase the height of the UILabel for those two cells only. I have tried using following code (also tried some other examples from this site).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UILabel *DetailLabel = (UILabel *)[cell.contentView viewWithTag:10];
UILabel *DetailText = (UILabel *)[cell.contentView viewWithTag:20];
if(indexPath.row == 3 || indexPath.row ==10){
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0;
DetailText.frame = CGRectMake(0, 0, 317, 80);
[DetailText sizeToFit];
}else{
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
}
[DetailLabel setText:[self.EmployerViewLabel objectAtIndex:[indexPath row]]];
return cell;
}
Unfortunately, the size of the UILable remained unchanged. Can anyone point out what I am doing wrong?
The problem is when you change the text in DetailText, you don't call [DetailText sizeToFit] again.
DetailText = [[UILabel alloc]initWithFrame:CGRectMake(60, 200, 100, 30)];
DetailText.backgroundColor = [UIColor orangeColor];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0; // Here is the key
DetailText.text = #"test message test message test message test message test message test message";
DetailText.frame = CGRectMake(100, 0, 100, 80);
[DetailText sizeToFit];
after when you change the text:
DetailText.text = #"this is the modified text";
[DetailText sizeToFit];
I tried this code and it resized.
From UILabel.h:
// this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
// truncated using the line break mode.
#property(nonatomic) NSInteger numberOfLines;
Just allow multiline UILabel:
...
[DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
DetailText.lineBreakMode = NSLineBreakByWordWrapping;
DetailText.font = [UIFont systemFontOfSize:17.0f];
DetailText.numberOfLines = 0; // Here is the key
DetailText.frame = CGRectMake(0, 0, 317, 80);
[DetailText sizeToFit];
...
You may want to set numberOfLines to 1 for other cases.

UITableViewCell showing double embedded UITextfields

I have a UITableView with regular UITableViewCell, but I don't use any of UITableViewCell's lables. I just use the cell to embed a label and a UITextField to input some data. Problem is when you scroll up or scroll down and the UITableviewCell redraws itself, it draws an overlapping UITextFieldView over the old one and you see doubles! I'm thinking that maybe since I do put these UITextFields into a dictionary, it might save the textfield with a strong pointer, and try to make another one and just overlap. Anyone have any suggestions?
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *product = [self.products objectAtIndex:indexPath.row];
NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];
cell.textLabel.text = #""; //black out text
CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];
productLabel.text = [[NSString alloc] initWithFormat:#"%#. %#",
[orderpoint objectForKey:#"sequence_nr"], [product objectForKey:#"name"]];
//word wrapping
productLabel.lineBreakMode = UILineBreakModeWordWrap;
productLabel.numberOfLines = 0; //infinite number of lines
productLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
[cell.contentView addSubview:productLabel];
//create the cell's textfield
UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
cellTextField.adjustsFontSizeToFitWidth = YES;
cellTextField.textColor = [UIColor blackColor];
cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
cellTextField.backgroundColor = [UIColor whiteColor];
cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
//cellTextField.delegate = self; //will need to set delegate, maybe
cellTextField.clearButtonMode = UITextFieldViewModeNever;
cellTextField.enabled = YES;
cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
cellTextField.delegate = self;
[cell.contentView addSubview: cellTextField]; //add the textfield to the cell
// save to dictionary, using a dictionary because not certain if this is created in order to use an Array
[self.textFieldDict setObject:cellTextField forKey:[[NSNumber alloc] initWithInteger:indexPath.row]];
return cell;
}
You have not initialized your cell.Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//It will check whether cell in there or not, then deque the cell...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *product = [self.products objectAtIndex:indexPath.row];
NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];
cell.textLabel.text = #""; //black out text
CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];
productLabel.text = [[NSString alloc] initWithFormat:#"%#. %#",
[orderpoint objectForKey:#"sequence_nr"], [product objectForKey:#"name"]];
//word wrapping
productLabel.lineBreakMode = UILineBreakModeWordWrap;
productLabel.numberOfLines = 0; //infinite number of lines
productLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
[cell.contentView addSubview:productLabel];
//create the cell's textfield
UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
cellTextField.adjustsFontSizeToFitWidth = YES;
cellTextField.textColor = [UIColor blackColor];
cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
cellTextField.backgroundColor = [UIColor whiteColor];
cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
//cellTextField.delegate = self; //will need to set delegate, maybe
cellTextField.clearButtonMode = UITextFieldViewModeNever;
cellTextField.enabled = YES;
cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
cellTextField.delegate = self;
[cell.contentView addSubview: cellTextField]; //add the textfield to the cell
return cell;
}
Either don't use reusability and always alloc the cell at each time or
make a check after dequeue (like this)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
// make text field and label an add tag
}
//and outside this by using tag fetch the labels and textField and clear the textFields.
You forgot to put the condition like this:
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
Used this as a reference: add subviews to UITableViewCell
I just first checked to see if this view was added from before, and if it was, then don't add it again. It has nothing to do with the cell being nil. Unless I missed something? all I know is that this seems to be working fine, now.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProductCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *product = [self.products objectAtIndex:indexPath.row];
NSDictionary *orderpoint = [self.orderpoints objectAtIndex:indexPath.row];
cell.textLabel.text = #""; //black out text
CGFloat calculatedHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel *productLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.bounds.size.width - 50.0, calculatedHeight)];
productLabel.text = [[NSString alloc] initWithFormat:#"%#. %#",
[orderpoint objectForKey:#"sequence_nr"], [product objectForKey:#"name"]];
//word wrapping
productLabel.lineBreakMode = UILineBreakModeWordWrap;
productLabel.numberOfLines = 0; //infinite number of lines
productLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
[cell.contentView addSubview:productLabel];
if (![cell viewWithTag:1])
{
//create the cell's textfield
UITextField *cellTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 50, cell.bounds.size.height - 30, 50, calculatedHeight - 20)];
cellTextField.adjustsFontSizeToFitWidth = YES;
cellTextField.textColor = [UIColor blackColor];
cellTextField.keyboardType = UIKeyboardTypeNumberPad; // will only need to end a count
cellTextField.returnKeyType = UIReturnKeyDone; // TODO make it go to the next items key, or make it exit out
cellTextField.backgroundColor = [UIColor whiteColor];
cellTextField.textAlignment = UITextAlignmentLeft; //align to the right
cellTextField.delegate = self; //will need to set delegate, maybe
cellTextField.clearButtonMode = UITextFieldViewModeNever;
cellTextField.enabled = YES;
cellTextField.borderStyle = UITextBorderStyleRoundedRect; //add bezel rounded look to textfield
cellTextField.delegate = self;
cellTextField.tag = 1; //set tag to 1
[cell.contentView addSubview: cellTextField]; //add the textfield to the cell
// save to dictionary, using a dictionary because not certain if this is created in order to use an Array
[self.textFieldDict setObject:cellTextField forKey:[[NSNumber alloc] initWithInteger:indexPath.row]];
}
return cell;
}

How to update multipe UILabel in table view

I need some help for my problem. I've seen a lot of similar posts, and tried solutions, but none of them have worked for me.
I have a custom UITableViewCell in which I have multiple UILabel and a UIButton control. This control is suppose to update one of the labels from its cell. However, instead of updating the correct UILabel it updates the UILabel in the last cell.
My code :
- (UITableViewCell *)tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSInteger ETag = 111;
static NSInteger STag = 112;
.... // code
if (cell == nil) {
.... // code
frame.origin.x = 85;
frame.origin.y = 10;
frame.size.height = 50;
frame.size.width = 100;
SLabel = [[UILabel alloc] initWithFrame:frame];
SLabel.tag = STag;
[cell.backgroundView addSubview:SLabel];
//[SLabel release];
frame.origin.x = 215;
frame.origin.y = 10;
frame.size.height = 50;
frame.size.width = 100;
ELabel = [[UILabel alloc] initWithFrame:frame];
ELabel.tag = ETag;
[cell.backgroundView addSubview:ELabel];
//[ELabel release];
.... // code
}
.... // code
ELabel = (UILabel *) [cell.backgroundView viewWithTag:ETag];
SLabel = (UILabel *) [cell.backgroundView viewWithTag:STag];
ELabel.text = [s._episode stringValue];
ELabel.backgroundColor = [UIColor clearColor];
ELabel.textColor = [UIColor whiteColor];
ELabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
ELabel.textAlignment = UITextAlignmentLeft;
ELabel.tag = 1000+indexPath.row;
SLabel.text = [s._season stringValue];
SLabel.backgroundColor = [UIColor clearColor];
SLabel.textColor = [UIColor whiteColor];
SLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
SLabel.textAlignment = UITextAlignmentLeft;
SLabel.tag = 1000+indexPath.row;
..... // code
return cell;
}
Thanks for reading and helping me.
Tommy
Thank you ! i resolved my problem with :
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UILabel *a = (UILabel *) [cell viewWithTag:(1000+indexPath.row)];
a.text = [NSString stringWithFormat:#"%#", #"something"];
the issue was that tags were not unique
You access needful UILabel via its tag, if it is unique in your view.
For example, you have UITableView with multiple cells. Each cell contains UILabel that might be updated. Let's set unique tags for that labels, let them be your indexPath.row of UITableViewCell (it is already done in your code).
Then when you want to update some cell with tag == cellsTagToUpdate you should just get reference to that cell via call to UITableView *tableView: UILabel *labelToUpdate = [tableView viewWithTag:cellsTagToUpdate].
Now you have reference to your label that you want to update. Before updating you should check if cell is not nil. It would be nil if that view (UILabel) is not now visible and was removed from superview.
Hmm you always can get a reference to you cells with UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; and then get the UILabel within the cell with viewWithTag and set the text of the label you want.
The first thing you do is get the indexPath from UITableViewDelegate tableView: didSelectRowAtIndexPath: selector and store it in some way (a property would suffice).
Then in your button selector could call cellForRowAtIndexPath: For the cell corresponding to indexPath selected in order to update the text of any of its UILabel 's and now:
ELabel = (UILabel *) [cell.backgroundView viewWithTag:ETag];
SLabel = (UILabel *) [cell.backgroundView viewWithTag:STag];
Edit: See Nektos answer
Nevertheless his way to layout and set the data is not how you should do this. He could do layout via IB and apply the standard pattern: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10

iPhone: Adding subviews to Cell ContentView overwrites cell texts on scrolling

I have used Cell.ContentView in my implementation for customizing the cell contents. It works fine but the only problem is when I have many cells and I scroll them up and down, the cell contents gets overwritten into the cells just become hidden followed by visible. Suppose I scroll first cell up and then again takes it down, the last cell's contents gets overwritten on first cell!!
I debugged enough on this but couldn't find the exact solution. I tried checking Cell.ContentView.SubViews count and if it 0 then only add other subviews. This doesn't display any cell contents until I scroll them up and down but once contents appeared, it doesn't overwrite..Little bit strange..!! I also made sure that I am using reusing the cell correctly. Following is my code that adds subviews into cell's contentview. Please let me know how could I get rid of this issue.
P.S: Don't worry about the variables and calculations I have done. Assume that it returns correct values. :)
- (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];
}
NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
NSInteger lableHeight = [[cellDetails objectForKey:#"TableItemTextFontSize"] intValue] * 2 + 10;
UILabel *textLabel = nil;
textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
textLabel.numberOfLines = 2;
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
if ([textLableColor scanHexInt:&hex]) {
textLabel.textColor = UIColorFromRGB(hex);
}
textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemTextFontName"] size:[[cellDetails objectForKey:#"TableItemTextFontSize"] intValue]];
[cell.contentView addSubview:textLabel];
textLabel.backgroundColor = [UIColor clearColor];
lableHeight = [[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue] * 2 + 10;
UILabel *detailTextLabel = nil;
detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
detailTextLabel.numberOfLines = 2;
detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
detailTextLabel.textAlignment = UITextAlignmentLeft;
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
if ([detailTextLableColor scanHexInt:&hex]) {
detailTextLabel.textColor = UIColorFromRGB(hex);
}
detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue]];
[cell.contentView addSubview:detailTextLabel];
detailTextLabel.backgroundColor = [UIColor clearColor];
return cell;
}
Thanks.
That is because toy are adding the views to your cell over and over again.
You should only add them when you create the cell and just set the labels when the cell is being reused.
You could set tags for your label, so that you can set it texts afterwards. The code bellow does the trick
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if(style == UITableViewCellStyleValue1)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
else
cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellIdentifier] autorelease];
NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
NSInteger lableHeight = [[cellDetails objectForKey:#"TableItemTextFontSize"] intValue] * 2 + 10;
UILabel *textLabel = nil;
textLabel.tag = 1;
textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
textLabel.numberOfLines = 2;
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
if ([textLableColor scanHexInt:&hex]) {
textLabel.textColor = UIColorFromRGB(hex);
}
textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemTextFontName"] size:[[cellDetails objectForKey:#"TableItemTextFontSize"] intValue]];
[cell.contentView addSubview:textLabel];
textLabel.backgroundColor = [UIColor clearColor];
lableHeight = [[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue] * 2 + 10;
UILabel *detailTextLabel = nil;
detailTextLabel.tag = 2;
detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
detailTextLabel.numberOfLines = 2;
detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
detailTextLabel.textAlignment = UITextAlignmentLeft;
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
if ([detailTextLableColor scanHexInt:&hex]) {
detailTextLabel.textColor = UIColorFromRGB(hex);
}
detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue]];
[cell.contentView addSubview:detailTextLabel];
detailTextLabel.backgroundColor = [UIColor clearColor];
} else {
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
UILabel *detailTextLabel = (UILabel *)[cell viewWithTag:2];
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
}
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
Make sure that dequeueReusableCellWithIdentifier and reuseIdentifier should be nil
Now it will work !!
Place all the cell UI content inside if(cell==nil){uilabel,uilabel or anythingUI related} ...since ui should be call only once at time of creating cel
To be clear, your problem is that as you scroll a cell into view you will often wind up with the text of the new cell being written over the top of a cell that was just scrolled offscreen?
The problem is that dequeueReusableCellWithIdentifier: doesn't "clean up" the cell (it calls prepareForReuse, but that's it), so all your old subviews are still in place. If you are going to reuse cells, you shouldn't be recreating these subviews each time. Instead, just adjust the properties of the existing subviews on a cell you get back from dequeueReusableCellWithIdentifier:, and only create the new subviews on a newly-allocated cell. Subclassing UITableViewCell can help with this, both to provide properties/ivars in which to hold references to these subviews and to organize the code a little nicer by moving the creation of subviews and the updating into appropriate methods. Or you could just not reuse cells, by passing nil for the reuse identifier.
The most likely reason that it didn't display any content until you scroll up and down is that a newly-created cell may have the framework-provided text field and image view, which may then be removed if the framework determines that you aren't actually using them.
In iOS5 UILineBreakModeWordWrap is deprecated. You should use NSLineBreakByWordWrapping instead.
This would change you code to look like detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping and textLabel.lineBreakMode = NSLineBreakByWordWrapping.

Access custom label property at didSelectRowAtIndexPath

I have a UILabel for each cell at cellForRowAtIndexPath.
UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
I want to access that string "myString" at didSelectRowAtIndexPath using indexpath.
NSString *anotherString = cell.textLabel.text;
returns null.
Now if at cellForRowAtIndexPath, I did something like cell.textLabel.text = theString; then the didSelectRowAtIndexPath returns the appropriate cell.
My question is, how can I access the text in the UILabel I apply to the cell, at didSelectRowAtIndexPath?
Also, logging the cell in didSelectRowAtIndexPath returns cell: <UITableViewCell: 0x5dcb9d0; frame = (0 44; 320 44); autoresize = W; layer = <CALayer: 0x5dbe670>>
Edit:
NSString *myString = [[results objectAtIndex:indexPath.row] valueForKey:#"name"];
//cell.textLabel.text = myString;
CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = 20;
CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];
[cell.contentView addSubview:cellLabel];
[cellLabel release];
return cell;
In this line of code:
NSString *anotherString = cell.textLabel.text;
How are you obtaining the cell? Is it nil? Also, the textLabel field you're accessing is the default label in the a UITableViewCell and not the label you are adding in -cellForRowAtIndexPath. Here is how you can get the cell from -didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];
}
The issue at this point, though, is that you can't access the UILabel by name, however, you can access it if you've set a tag. So, when you create your UILabel, set the tag like this:
UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];
// Set the tag to any integer you want
cellLabel.tag = 100;
[cell.contentView addSubview:cellLabel];
[cellLabel release];
So, now you can access the UILabel by tag:
- (void)tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];
UILabel *label = (UILabel*)[cell viewWithTag:100];
NSLog(#"Label Text: %#", [label text]);
}
Can you post the code that you assign the cellLabel to the cell? Did you do something like this: cell.textLabel = cellLabel?
Look for UITableViewCell for more details