UITableView's viewForHeaderInSection method: accessing a cell's visual properties - ios

I am defining a header row for my UITableView using the viewForHeaderInSection delegate method, and I wanted to get the X coodinate from the first row to position some labels, because I'm moving about the labels in the row constantly and would like to define those coordinates in the storyboard. But it's not working: the X coordinate is always 0. Here is the attempt:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
float x = tableView.frame.origin.x;
float y = tableView.frame.origin.y;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y - 22, tableView.bounds.size.width, 12)];
view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
view.userInteractionEnabled = YES;
view.backgroundColor = [UIColor lightGrayColor];
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_DATE.frame.origin.x, 0, 65, 22)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont systemFontOfSize:12.0];
dateLabel.text = #"DATE";
UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_LOC.frame.origin.x, 0, 65, 22)];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:12.0];
locationLabel.text = #"LOC";
[view addSubview:dateLabel];
[view addSubview:locationLabel];
return view;
}
Is there some way to properly get a row from the UITableView to ascertain the X/Y coordinates of the cell's labels?

This line:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];
is the same as:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:nil];
You aren't passing in an index path:
Try this:
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

Related

UITableview overwriting on olddata

hi im creating chat example and if user add send button adding new object in my arrray and reload data but its overwrite old table.
its my array :
arr1 = [[NSMutableArray alloc] initWithObjects:#"Whats up today ? ", #"Hello.", nil];
Here my source :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simleTableIdentifier = #"TeamCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TeamCell"];
}
cell.backgroundColor = [UIColor clearColor];
tableView.scrollEnabled = YES;
UIImageView *myRect = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 10, 12)];
myRect.image = [UIImage imageNamed:#"tri.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"jj.jpg"]];
UILabel *label = [[UILabel alloc] init];
label.text = [arr1 objectAtIndex:indexPath.row];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font =[UIFont systemFontOfSize:14.0];
label.text = [arr1 objectAtIndex: indexPath.row];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(40, 40, 210, 80);
[label sizeToFit];
UIImageView *imgim = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, label.frame.size.width+20, label.frame.size.height+20)];
imgim.image = [UIImage imageNamed:#"bg.png"];
CGRect myRectANGLE = CGRectMake(imgim.frame.origin.x+10,imgim.frame.origin.y+10, label.frame.size.width, label.frame.size.height);
[label setFrame:myRectANGLE];
CALayer *addRadius = [imgim layer];
[addRadius setMasksToBounds:YES];
[addRadius setCornerRadius:5.0];
[self.MyTableView setSeparatorColor:[UIColor clearColor]];
[self.MyTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
self.MyTableView.transform = CGAffineTransformMakeScale(1, -1); //in viewDidLoad
cell.transform = CGAffineTransformMakeScale(1, -1);//in tableView:cellForRowAtIndexPath:
[[cell contentView] addSubview:myRect];
[[cell contentView] addSubview:imgim];
[[cell contentView] addSubview:label];
return cell;
}
İf click send button i reload data but its overwite on old-data look like my photo :
-(IBAction)btnTap:(UIButton *)sender {
[arr1 insertObject:Textify.text atIndex:0];
NSLog(#"%i", arr1.count);
[MyTableView reloadData];
}
Try clearing the cell's contentView subview, and then add the new subviews.
Here's a block of code for removing the subviews, taken from this post.
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
UITableViewCell will be reused while reload, you should not create subviews again to add on cell.contentView.Try to use code below
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TeamCell"];
UILabel *label = [[UILabel alloc] initWithFrame:someFrame];
label.tag = 1000;
[cell.contentView addSubview:label];
}
UILabel *label = [cell.contentView viewWithTag:1000];
label.text = #"something";

UIlabel with Custom cell not appear

I'm confused !, it is not my first time to create Custom Cell but this time UILabel doesn't appear I don't know where is the problem ?
This the custom cell Cell.m
mainView = [[UIView alloc] initWithFrame:CGRectZero];
mainView.backgroundColor = [UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
profile = [[UIImageView alloc] initWithFrame:CGRectZero];
profile.layer.masksToBounds = YES;
profile.layer.cornerRadius = 22.5;
profile.layer.borderColor = [UIColor whiteColor].CGColor;
profile.layer.borderWidth = 1.0f;
profile.clipsToBounds = YES;
tweet_is = [[UILabel alloc] initWithFrame:CGRectZero];
tweet_is.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:20];
tweet_is.backgroundColor = [UIColor clearColor];
tweet_is.textColor = [UIColor blackColor];
[self.mainView addSubview:tweet_is];
[self.mainView addSubview:profile];
[self.contentView addSubview:self.mainView];
-(void)layoutSubviews {
[super layoutSubviews];
self.mainView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 10.0f);
self.profile.frame = CGRectMake(15, 15, 45, 45);
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
}
This is the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tweet_is.text = #"Not approve";
return cell;
}
Change this line:
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
set it self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 30.0f);
If your cell height is less than 300 (I think it is) then you can not see the text. Let me know if that helps.. :)

How to optimize cellForRowAtIndexPath: code

After adding a font and a shadow to some UILabels I noticed that the table view animation lags when the view is popped off the stack (a side swipe like FB/Path uses). The side swipe was smooth until I added the UILabel shadows.
I think I might be adding it it the wrong place so that the label properties are being added incorrectly maybe. Please take a look at the following cellForRowAtIndexPath: method below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
thanks for any help.
You're always adding new subviews. So whenever you scroll the table view your cells are getting more and more content added into them.
Create all your subviews when the cell is created and then just update the subviews settings. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.tag = 123123;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
Since text attributes never change, move the code that sets them inside the if statement. Keep only the code that sets the image and the text of your labels outside the if statement. Cells are reused, so the attributes such as font etc. will remain with the cell even after it gets "recycled". In the else branch add code that finds existing labels in the cell. Otherwise, you keep adding the same label to the cell multiple times.
You add subviews, even after dequeueing instead of initializing a new cell. Make sure all the code that creates and adds subviews are only done on initializing a cell. If you need to refer to views in the cell for configuring, subclass UITableViewCell.
Also, shadow rendering could be slowing it down too, add a shadowpath to make the rendering more efficient:
add to your tableView:cellForRowAtIndexPath: method:
...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...
This article from Twitter Engineering gives you a great overview: http://engineering.twitter.com/2012/02/simple-strategies-for-smooth-animation.html
Basically, you want to avoid using subviews but instead draw your content directly with Quartz. It's the single best thing you can do to improve performance. Also: Avoid transparency! In Instruments, you can also use the Core Animation instrument and activate "Color Blended Layers" to see where transparent views are being composed:
Replace your code with this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
imageView.tag =1;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.tag = 2;
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 3;
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
}
UIImageView *tempImgView = (UIImageView *)[cell viewWithTag:1];
tempImgView.image = [UIImage imageNamed:#""];// Here you can set any image by reusing imageview without allocating again and again
UILabel *tempLabel;
tempLabel = (UILabel *)[cell viewWithTag:2];
tempLabel.text = #"";// Here you can access your title label and can set its properties without allocating again
tempLabel = (UILabel *)[cell viewWithTag:3];
tempLabel.text = #"";// Here you can access your detailLabel label and can set its properties without allocating again
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
You need create custom table view cell with a class. This code you added many label then shadow show no correctly.
code like this.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"rest.jpg"];
cell.titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}

Label in UITableCellView.contentView showing two times when using autoresizing

I'm using Grouped UITableView and add UITableViewCells two UILabel's and adding them to the contentView of the cell. I am also implementing edit mode in my app on this UITableView.
I use the
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; in order to decrease width of the text so the delete red button showing in edit mode will not cover my text.
Now what I see is when I scroll my table not in edit mode the labels show two times as if it is in in edit mode and out of edit mode at the same time.
This is the code:
- (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];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblDetail.frame = DetailLabelFrame;
lblDetail.lineBreakMode = UILineBreakModeWordWrap;
lblDetail.backgroundColor = [UIColor clearColor];
lblDetail.numberOfLines = 2;
lblTitle.frame = TitleLabelFrame;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.frame = TitleLabelFrame;
[lblDetail setFont:[UIFont fontWithName:#"Arial" size:14]];
[lblTitle setFont:[UIFont fontWithName:#"Arial-BoldMT" size:15]];
lblDetail.textAlignment = UITextAlignmentRight;
lblTitle.textAlignment = UITextAlignmentRight;
lblTitle.text = #"054-9700583";
lblDetail.text = #"שאלה:במה אפשר לעזור לך?";
[cell.contentView addSubview:lblDetail];
[cell.contentView addSubview:lblTitle];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.image = [UIImage imageNamed:#"girl.png"];
[lblDetail release];
[lblTitle release];
return cell;
}
- (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];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblDetail.frame = DetailLabelFrame;
lblDetail.lineBreakMode = UILineBreakModeWordWrap;
lblDetail.backgroundColor = [UIColor clearColor];
lblDetail.numberOfLines = 2;
lblTitle.frame = TitleLabelFrame;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.frame = TitleLabelFrame;
[lblDetail setFont:[UIFont fontWithName:#"Arial" size:14]];
[lblTitle setFont:[UIFont fontWithName:#"Arial-BoldMT" size:15]];
lblDetail.textAlignment = UITextAlignmentRight;
lblTitle.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:lblDetail];
[cell.contentView addSubview:lblTitle];
lblDetail.tag = 10;
lblTitle.tag = 11;
}
lblDetail = (UILabel*)[cell.contentView viewWithTag:10];
lblTitle = (UILabel*)[cell.contentView viewWithTag:11];
lblTitle.text = #"054-9700583";
lblDetail.text = #"שאלה:במה אפשר לעזור לך?";
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.image = [UIImage imageNamed:#"girl.png"];
[lblDetail release];
[lblTitle release];
return cell;
}
Your cell contents was overlapping may be it will help you.Still now if you face same problem the knock me without any hesitation.
If this is only happening when you're scrolling it may be because when a cell is being reused it's creating a new set of labels in addition to what's already in the reused cell.
What you can try to do is move these two lines into your if (cell == nil ) block:
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
That way when a cell is being reused it'll continue to use the existing lblDetail and lblTitle instead of creating a new one.

UItableView multiplle data printed

I am creating grouped table in my application.
It works fine. But when I scroll the table upward and backward reprinted text on previous cell. It look like horrible.
This is my Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
/*
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
*/
if(indexPath.section == 0)
{
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
float rowHeight = [DetailViewController calculateHeightOfTextFromWidth:[dict objectForKey:#"golf_name"] :[UIFont boldSystemFontOfSize:18] :290 :UILineBreakModeTailTruncation];
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, rowHeight)];
categorName.font = [UIFont boldSystemFontOfSize:20];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor whiteColor];
categorName.text = [dict objectForKey:#"golf_name"];
[cell addSubview:categorName];
[categorName release];
}
if(indexPath.section == 1)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 70, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Phone";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 290, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_phone"];
[cell addSubview:phone];
[phone release];
}
if(indexPath.section == 2)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 90, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Home page";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 230, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_url"];
[cell addSubview:phone];
[phone release];
}
.......
.....
return cell;
}
Update
My original answer was not the correct way to reuse UITableViewCell's. You should never use "CellID%d%d"(indexPath.row & indexPath.column) as cell identifier, that defeats the very purpose of cell reuse. It will create separate UITableViewCell for every row in the table, and every one of those cells stays in memory. Imagine what will happen if you have a tableView with around 1000 rows.
The real answer to OP's question is - make sure to reset a UITableViewCell returned by dequeueReusableCellWithIdentifier because it might return a cell that is already used and its UI populated. For example, if you didn't reset the texts in UILabel, it might contain strings that should have been shown in a different row of your tableView.
My original Answer (Don't use this)
Hiren, just try,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
}
instead of
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
Please read this..you will understand how to give cell identifiers..
When you create the UITableView object, Set the rowHeight for each cell.
myTableView.rowHeight = 50; //Depend on your requirement,
Let me know if you still struggle to get the desire result.
If your cell's height is not constant then implement heightForRowAtIndexPath method,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And return different-2 height for each cell.
try adding any color because clearcolor is making label transparent.

Resources