I'm very much new to iphone development and strucked at a point. Actually my need is I'm displaying some results in a UITableView and I'm displaying then UILables of UITableView's cell..
How can I make the UILabel's to adjust the content according to the text. Actually text is not static .It may be changing at run time.So I need to set dynamic size to a UILabel .Ans secondly suppose if the text is null then it should not show any space and immediately the next UILabel should start .How can I make it possible ?
Here is my code..
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentHtmlElement==#"3")
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
name1.font=[UIFont boldSystemFontOfSize:16];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:#"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
codetype .font=[UIFont boldSystemFontOfSize:12];
[codetype setTextAlignment:UITextAlignmentLeft];
[codetype setText:[d valueForKey:#"Code"]];
codetype.tag=112;
[cell addSubview:codetype];
[codetype release];
UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
id .font=[UIFont fontWithName:#"arial" size:12];
[id setTextAlignment:UITextAlignmentLeft];
[id setText:[d valueForKey:#"ID"]];
id.tag=113;
[cell id ];
[id release];
UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
[address release];
UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
[city release];
}
Here I have set all the UILabels to static size.But what I need is dynamic size.and suppose if the address label is null then it should not show any space and next label i.e city should start immediately next after id ..How is it possible?
I did this task in my code.... you can use this code and do changes according to your code....and let me know if you get any problem.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//code to get notification text height
NSString *text = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 100.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//label for message
UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
lblMsg.tag = 12;
lblMsg.backgroundColor = [UIColor clearColor];
[lblMsg setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:lblMsg];
}
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
NSString *txt = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 2000.0f);
CGSize size = [txt sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//set frame height to labelMsg
UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
lblMsg.frame = CGRectMake(15,25, 175, size.height);
[lblMsg setNumberOfLines:size.height/16];
//set labelMsg text
lblMsg.text = [dicNotification objectForKey:#"message"];
return cell;
}
The thing is that you must use heightForRowAtIndexPath. The problem is that it is called prior to cellForRowAtIndexPath, therefore the text is not in the component and you can not simply adjust the component size to the text. So, what you do is calculate the text size using a sizeWithFont.
In case of normal/standard text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSString* theText = ... ;
//then calculate the size. systemFontOfSize depends on your font size
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
return textSize.height;
}
In the case of attributed text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSAttributedString* theText = ...;
//then calculate the size.
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];
return rectSize.size.height;
return textSize.height;
}
you can make UILable adjust content with bellow code
CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font];
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
and here set second lable like bellow..
UPDATE
if([[d valueForKey:#"address"] isEquelToString:#""]){
UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font];
city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
else{
UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font];
address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
and so on for every label..
follow this logic for whole label
i hope this help you...
Related
I m a newbie to iphone development and this is my first question.
I m displaying a UITableView in UIScrollView and I could not specify the height for both tableView and scrollView because the data is not static and it comes from services.
I need scrollView to set its height dynamically according to the content(height)of tableView.
I have done this but I couldn't see the last cell complete data.
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,327,480) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[testscroll addSubview:self.tableView];
[self.tableView reloadData];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.contentSize.height);
float ftbl = self.tableView.frame.origin.y + self.tableView.contentSize.height + 150;
scrollView.contentSize=CGSizeMake(320, ftbl);
scrollView.scrollEnabled=YES;
Im calculating height of tableview :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView: self.tableView cellForRowAtIndexPath: indexPath];
NSString *city = lblcity.text;
UIFont *font = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds = [city sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSString *state = lblstate.text;
UIFont *font1 = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize1 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds1 = [state sizeWithFont:font1 constrainedToSize:constraintSize1 lineBreakMode:NSLineBreakByWordWrapping];
NSString *name = lblname.text;
UIFont *font2 = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize2 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds2 = [name sizeWithFont:font2 constrainedToSize:constraintSize2 lineBreakMode:NSLineBreakByWordWrapping];
NSString *address = lbladdress.text;
UIFont *font3 = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize3 = CGSizeMake(280.0f, MAXFLOAT);
CGSize bounds3 = [address sizeWithFont:font5 constrainedToSize:constraintSize3 lineBreakMode:NSLineBreakByWordWrapping];
return (CGFloat) cell.bounds.size.height + bounds.height+bounds1.height+bounds2.height+bounds3.height;
}
How can scrollView increase/decrease its height according to the height of UItableview?
EDIT:
-(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];
lblname = [[UILabel alloc] initWithFrame:CGRectZero];
lblname.tag = 111;
lblname.backgroundColor = [UIColor clearColor];
[lblname setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
[lblname setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview:lblname];
lbladdress = [[UILabel alloc] initWithFrame:CGRectZero];
lbladdress.tag = 113;
lbladdress.backgroundColor = [UIColor clearColor];
[lbladdress setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[lbladdress setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lbladdress];
lblcity = [[UILabel alloc] initWithFrame:CGRectZero];
lblcity.tag = 115;
lblcity.backgroundColor = [UIColor clearColor];
[lblcity setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[lblcity setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lblcity];
lblstate=[[UILabel alloc] initWithFrame:CGRectZero];
lblstate.tag = 116;
lblstate.backgroundColor = [UIColor clearColor];
[lblstate setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[lblstate setLineBreakMode:NSLineBreakByWordWrapping];
[cell addSubview: lblstate];
}
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [providerarray objectAtIndex:indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *name2 = [d objectForKey:#"Name"];
CGSize constraint1 = CGSizeMake(175, 2000.0f);
CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:#"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
lblname = (UILabel *)[cell viewWithTag:111];
lblname.text = [NSString stringWithFormat:#"%#",name2];
[lblname setNumberOfLines:0];
lblname.frame = CGRectMake(105,25, size1.width, size1.height);
NSString *lane2 = [d objectForKey:#"Address"];
CGSize constraint3 = CGSizeMake(175, 2000.0f);
CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint3 lineBreakMode:NSLineBreakByWordWrapping];
lbladdress = (UILabel *)[cell viewWithTag:113];
lbladdress.text = [NSString stringWithFormat:#"%#",lane2];
[lbladdress setNumberOfLines:0];
lbladdress.frame = CGRectMake(105,lblname.frame.size.height+lblname.frame.origin.y, size3.width, size3.height);
NSString *city2 =[d objectForKey:#"City"];
NSString *trimmedString = [city2 stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", trimmedString);
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [trimmedString sizeWithFont: [UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint5 lineBreakMode:NSLineBreakByWordWrapping];
lblcity = (UILabel *)[cell viewWithTag:115];
lblcity.text = [NSString stringWithFormat:#"%#",trimmedString];
[lblcity setNumberOfLines:0];
lblcity.frame = CGRectMake(105,lbladdress.frame.size.height+lbladdress.frame.origin.y, 175, size5.height);
NSString *state1=[d objectForKey:#"State"];
CGSize constraint6=CGSizeMake(175,2000.0f);
CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint6 lineBreakMode:NSLineBreakByWordWrapping];
state=(UILabel *)[cell viewWithTag:116];
[state setText:[NSString stringWithFormat:#"%#",state1]];
state.frame=CGRectMake(105, lblcity.frame.size.height+lblcity.frame.origin.y, size6.width, size6.height);
[state setTextAlignment:NSTextAlignmentLeft];
[state sizeToFit];
return cell;
}
I would observe the table view's contentSize property and adjust your scroll view's height based on that. For example:
Suppose you have your table view. You can observe its content size like so:
[self.tableView addObserver:self forKeyPath:#"contentSize" options:0 context:NULL];
Now, you can add this method to your view controller:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
}
Inside of this method, you can add some logic to see what observed value was changed, and then adjust your scroll view's height.
if (object == self.tableView) {
if ([keyPath isEqualToString:#"contentSize"]) {
CGRect scrollViewFrame = self.scrollView.frame;
scrollViewFrame.height = self.tableView.contentSize.height;
[self.scrollView setFrame:scrollViewFrame];
}
}
Finally you can remove the observer in dealloc.
-(void)dealloc {
[self.tableView removeObserver:self forKeyPath:#"contentSize"];
}
I have to place two UILabels(state,zipCode) side by side i.e California 32320 in UITableView's cell.I can see them clearly when the fontsize is less.When I increase the font-size I couldnot see the last letter of state label and zipCode label is getting added.This is the code I m working on:
-(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];
state=[[UILabel alloc] initWithFrame:CGRectZero];
state.tag = 116;
state.backgroundColor = [UIColor clearColor];
[state setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[state setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:state];
zipCode=[[UILabel alloc] initWithFrame:CGRectZero];
zipCode.tag = 121;
zipCode.backgroundColor = [UIColor clearColor];
[zipCode setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[zipCode setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:zipCode];
}
NSString *state1=[d objectForKey:#"State"];
CGSize constraint6=CGSizeMake(175,2000.0f);
CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint6 lineBreakMode:UILineBreakModeWordWrap];
state=(UILabel *)[cell viewWithTag:116];
state.frame=CGRectMake(105, city.frame.size.height+city.frame.origin.y+5, size6.width, 10);
[state setTextAlignment:UITextAlignmentLeft];
[state setText:[d valueForKey:#"State"]];
NSString *zip = [d objectForKey:#"Zip"];
if([zip isEqualToString:#""])
{
zipCode=[[UILabel alloc]initWithFrame:CGRectMake(105, 125, 320,10)];
zipCode .font=[UIFont fontWithName:#"Helvetica" size:12];
[zipCode setTextAlignment:UITextAlignmentLeft];
zipCode.hidden=YES;
[zipCode release];
}
else
{
NSString *zip=[d objectForKey:#"Zip"];
CGSize constraint200=CGSizeMake(175,2000.0f);
CGSize size200=[zip sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap];
zipCode=(UILabel *)[cell viewWithTag:121];
zipCode.frame=CGRectMake(13+state.frame.size.width+state.frame.origin.x, city.frame.size.height+city.frame.origin.y+5, size200.width,10);
[zipCode setTextAlignment:UITextAlignmentLeft];
[zipCode setText:[d valueForKey:#"Zip"]];
zipCode.numberOfLines=0;
}
return cell;
}
Now I can see the result as
Californi 32320
But when I reduce the font-size to 10 then I can clearly see them (California 32320).But I need the font-size to be 12 only as per my requirement.
Where I m going wrong?
Use this code it will saw the label in fit to size of your label
label.adjustsFontSizeToFitWidth = YES;
Please update the Label width with some what large. And use the "setNoOfLine" property of Lable to 2. So that, it will display text in 2 lines.
Hope it will be helpful to you.
Cheers!
There is a little trick by which the label will auto resize according to your text
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,200,40)];//set your frame
testLabel.numberOfLines = 0;//Important to add this line
testLabel.font = [UIFont boldSystemFontOfSize:15];//Set your font
testLabel.text = #"Set your text here!";
[testLabel sizeToFit]; // this line will do the trick :)
The font of your state label is Helvetica-Bold, but in your sizeWithFont, the font you specified is Helvetica. Alternatively, you can just specify state.font.
Do like this,
CGFloat lLabelWidth = [yourText sizeWithFont: factLabel.font].width;
CGRect _tempFrame=yourLabel.frame;
_tempFrame.size.width=lLabelWidth;
yourLabel.frame=_tempFrame;
hope it will helps you....
i've been trying around for a while now and cant seem to find a solution. i am using a UITableViewCellStyleDefault cellstyle in my tableview, and am trying to get the font to resize when the text gets too long.
cell creation
static NSString *CellIdentifier = #"thisMonthCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.textLabel setAdjustsFontSizeToFitWidth:YES];
[cell.textLabel setMinimumFontSize:14];
UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 10, 100, 20)];
[valueLabel setBackgroundColor:[UIColor clearColor]];
valueLabel.tag = 1001;
[valueLabel setTextAlignment:UITextAlignmentRight];
[cell addSubview:valueLabel];
}
Expense *expense = [[self.dataHandler monthExpenses]objectAtIndex:indexPath.row];
UILabel *value = (UILabel*)[cell viewWithTag:1001];
[cell.textLabel setText:[expense name]];
if ([[expense value]doubleValue] > 0) {
[value setText:[NSString stringWithFormat:#"+%.2f",[[expense value]doubleValue]]];
[value setTextColor:[self greenColor]];
}
else{
[value setText:[NSString stringWithFormat:#"%.2f",[[expense value]doubleValue]]];
[value setTextColor:[self redColor]];
}
return cell;
but somehow the textLabel wont resize if the text is too long.
here is a screenshot demonstrating the problem:
any ideas?
UPDATE i managed to achieve my goal by removing the standardLabel and adding a custom one,.. weird that it would not work with the standard one.
Try this cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap
Use following two line of code also when you are creating value label.
valueLabel.lineBreakMode = UILineBreakModeWordWrap;
valueLabel.numberOfLines = 0;
EDITED-
To change the height of cell -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = #"oooooooooooooooooooo"; //Text that you are using
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:16.0]; //Whatever font you are using.
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 25.0; //25.0 is offset, you can change as per need
}
im having troubles making my UILabel height dynamic. The label is contained in a custom TableViewCell
I have managed to make the TableViewCellHeight dynamic, but i cant make the label multiple lines.
Whenever i set number of lines = 0 (in IB as well as code), the label/text disappears completely. And lineBreakMode doesnt do much either?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
//return 66;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.eventDataUp count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"EventUpCVCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
self.eventUpCVCell = nil;
[[NSBundle mainBundle] loadNibNamed:#"EventUpCVCell" owner:self options:nil];
cell = self.eventUpCVCell;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
UILabel *lblMonth = (UILabel *) [cell viewWithTag:kLabel1Tag];
UILabel *lblTitle = (UILabel *) [cell viewWithTag:kLabel2Tag];
UILabel *lblDate = (UILabel *) [cell viewWithTag:kLabel3Tag];
//month.text = eventUp.month;
lblTitle.text = eventUp.title;
lblDate.text = eventUp.date;
//lblDate.lineBreakMode = UILineBreakModeWordWrap;
lblDate.numberOfLines = 1;
lblMonth.text = [NSString stringWithFormat:#"%# ", eventUp.month];
//cell.textLabel.text = eventUp.title;
//cell.detailTextLabel.text = eventUp.date;
//NSLog(#"Hello");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
You have to set the label frame after creating it, actually you create it with CGRectZero Frame
try:
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0,size.width,size.height)];
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