Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2011, 03:32 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default Problem with Custom cells

I have a problem with custom cells. I watch most of the tutorials, some of them uses Database, some uses Plist. But i wanted to use array instead.

I am able to use array on the custom cells, but when I increase the height of the cell, it started to give me problems.

the problem is, the first and the last cell will overlap each other. the pictures and the name will over lap.

here's my code
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (tableView.tag == 1) {
        return [arrayME count];
    } 
    else if (tableView.tag == 2) {
        return [arrayYOU count];
    } 
    else {
        return 0;
    }
   
}
this code is for using 2 table with different array in a same view


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];
        
       
    }
    if(tableView.tag ==1){
   
        
        
        CGRect nameValueRect = CGRectMake(130, 15, 200, 40);
        UILabel *nameValue = [[UILabel alloc] initWithFrame: 
                              nameValueRect];
		nameValue.backgroundColor = [UIColor clearColor];
		nameValue.textColor = [UIColor whiteColor];
		nameValue.font = [UIFont boldSystemFontOfSize:40];
        nameValue.text = [arrayME objectAtIndex:indexPath.row];
        [cell.contentView addSubview:nameValue];
        [nameValue release];
        
		CGRect colorValueRect = CGRectMake(200, 60, 500, 30);
        UILabel *colorValue = [[UILabel alloc] initWithFrame: 
							   colorValueRect];
		colorValue.backgroundColor = [UIColor clearColor];
		colorValue.font = [UIFont boldSystemFontOfSize:15];
        nameValue.text = [arrayYOU objectAtIndex:indexPath.row];
        colorValue.text = [arrayYOU objectAtIndex:indexPath.row];
        [cell.contentView addSubview:colorValue];
        [colorValue release];	
		
		NSUInteger row = [indexPath row];
		if (row ==0)
		{
			UIImage *image = [UIImage imageNamed:@"pork.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];
		}
		if (row ==1)
		{
			UIImage *image = [UIImage imageNamed:@"beef.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];			
		}
        

    }
    else if(tableView.tag ==2) {
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayWE objectAtIndex:indexPath.row]];
    }
   
    // Configure the cell...
    
    return cell;
}
when i started to add the code in, it started to give me the problem I mention above. I thinking, is it because of the reusable cell problem?.
Help pls.
eagleclaws is offline   Reply With Quote
Old 05-26-2011, 04:41 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Can anyone help me please.
eagleclaws is offline   Reply With Quote
Old 05-26-2011, 03:21 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I dont see any code where you are changing hte height of the cell. Note that you should be using the heightForRowAtIndexPath method if you arent.
smithdale87 is offline   Reply With Quote
Old 05-26-2011, 05:55 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
I dont see any code where you are changing hte height of the cell. Note that you should be using the heightForRowAtIndexPath method if you arent.
For the height of the cell I changed it in the .xib file.
I know I can use heightForRowAtIndexPath
If I use heightForRowAtIndexPath the return part will be like this
Code:
 if (tableView.tag == 1 )
             {  return 55;
} 
Else return 0;
But using the heightRowAtIndexPath and changing it at .xib file , both have same problem. Which is the cell will overlap each other when I scroll it.
eagleclaws is offline   Reply With Quote
Old 05-26-2011, 06:01 PM   #5 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Your "colorValueRect" goes way beyond a height of 55. You have it start at 60 with a height of 30, so the bottom will draw at 90, which will overlap the next cell by 45. If you are using a cellHeight of 55, everything must fit in that height.
__________________
My development blog: http://jrinn.com
JasonR is offline   Reply With Quote
Old 05-26-2011, 08:32 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Quote:
Originally Posted by JasonR View Post
Your "colorValueRect" goes way beyond a height of 55. You have it start at 60 with a height of 30, so the bottom will draw at 90, which will overlap the next cell by 45. If you are using a cellHeight of 55, everything must fit in that height.
I tried what you said but it still overlaps

this is the screen shot of what i say, the top three pictures are the one when i add the code i mentioned. the arrow beside means scrolling up or down. when i run the program with the code it will show like the first picture. when i scroll down it will be like the second picture, the when i scroll up again. it will become like the third picture.
i want to make it like the fourth and fifth picture with the first three picture without this overlapping problem

Last edited by eagleclaws; 05-26-2011 at 08:36 PM.
eagleclaws is offline   Reply With Quote
Old 05-27-2011, 02:39 AM   #7 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

try with
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];
        
       
    }else{

       UILabel *nameValue = (UILabel *)[cell viewWithTag:200];
       UILabel *colorValue =  (UILabel *)[cell viewWithTag:300];

       if(nameValue)
           [nameValue removeFromSuperview];

       if(colorValue)
            [colorValue  removeFromSuperview];
   }

    if(tableView.tag ==1){
   
        
        
        CGRect nameValueRect = CGRectMake(130, 15, 200, 40);
        UILabel *nameValue = [[UILabel alloc] initWithFrame: 
                              nameValueRect];
		nameValue.backgroundColor = [UIColor clearColor];
		nameValue.textColor = [UIColor whiteColor];
		nameValue.font = [UIFont boldSystemFontOfSize:40];
                nameValue.tag = 200;
        nameValue.text = [arrayME objectAtIndex:indexPath.row];
        [cell.contentView addSubview:nameValue];
        [nameValue release];
        
		CGRect colorValueRect = CGRectMake(200, 60, 500, 30);
        UILabel *colorValue = [[UILabel alloc] initWithFrame: 
							   colorValueRect];
		colorValue.backgroundColor = [UIColor clearColor];
		colorValue.font = [UIFont boldSystemFontOfSize:15];
        nameValue.text = [arrayYOU objectAtIndex:indexPath.row];
        colorValue.text = [arrayYOU objectAtIndex:indexPath.row];
        colorValue.tag = 300;
        [cell.contentView addSubview:colorValue];
        [colorValue release];	
		
		NSUInteger row = [indexPath row];
		if (row ==0)
		{
			UIImage *image = [UIImage imageNamed:@"pork.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];
		}
		if (row ==1)
		{
			UIImage *image = [UIImage imageNamed:@"beef.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];			
		}
        

    }
    else if(tableView.tag ==2) {
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayWE objectAtIndex:indexPath.row]];
    }
   
    // Configure the cell...
    
    return cell;
}
__________________

Last edited by dany_dev; 05-27-2011 at 03:51 AM.
dany_dev is offline   Reply With Quote
Old 05-27-2011, 03:00 AM   #8 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
try with
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];
        
       
    }else{

       UILabel *nameValue =  [cell viewWithTag:200];
       UILabel *colorValue =  [cell viewWithTag:300];

       if(nameValue)
           [nameValue removeFromSuperview];

       if(colorValue)
            [colorValue  removeFromSuperview];
   }

    if(tableView.tag ==1){
   
        
        
        CGRect nameValueRect = CGRectMake(130, 15, 200, 40);
        UILabel *nameValue = [[UILabel alloc] initWithFrame: 
                              nameValueRect];
		nameValue.backgroundColor = [UIColor clearColor];
		nameValue.textColor = [UIColor whiteColor];
		nameValue.font = [UIFont boldSystemFontOfSize:40];
                nameValue.tag = 200;
        nameValue.text = [arrayME objectAtIndex:indexPath.row];
        [cell.contentView addSubview:nameValue];
        [nameValue release];
        
		CGRect colorValueRect = CGRectMake(200, 60, 500, 30);
        UILabel *colorValue = [[UILabel alloc] initWithFrame: 
							   colorValueRect];
		colorValue.backgroundColor = [UIColor clearColor];
		colorValue.font = [UIFont boldSystemFontOfSize:15];
        nameValue.text = [arrayYOU objectAtIndex:indexPath.row];
        colorValue.text = [arrayYOU objectAtIndex:indexPath.row];
        colorValue.tag = 300;
        [cell.contentView addSubview:colorValue];
        [colorValue release];	
		
		NSUInteger row = [indexPath row];
		if (row ==0)
		{
			UIImage *image = [UIImage imageNamed:@"pork.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];
		}
		if (row ==1)
		{
			UIImage *image = [UIImage imageNamed:@"beef.png"];
			cell.imageView.image = image;
			cell.textLabel.font = [UIFont boldSystemFontOfSize:40];			
		}
        

    }
    else if(tableView.tag ==2) {
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayWE objectAtIndex:indexPath.row]];
    }
   
    // Configure the cell...
    
    return cell;
}
Code:
UILabel *nameValue =  [cell viewWithTag:200];
UILabel *colorValue =  [cell viewWithTag:300];
it gives Incompatible Objective - C types initializing 'struct UIView *', expected 'struct UILabel *' warning.

is it because I didnt declare a UILabel at the .h class file.

I follow the code from other people source code, they didnt declare any UILabel at their .h class file

Last edited by eagleclaws; 05-27-2011 at 03:02 AM.
eagleclaws is offline   Reply With Quote
Old 05-27-2011, 03:50 AM   #9 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

i forgot the cast
Code:
UILabel *nameValue =  (UILabel *)[cell viewWithTag:200];
UILabel *colorValue =   (UILabel *)[cell viewWithTag:300];
__________________
dany_dev is offline   Reply With Quote
Old 05-27-2011, 04:30 AM   #10 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
i forgot the cast
Code:
UILabel *nameValue =  (UILabel *)[cell viewWithTag:200];
UILabel *colorValue =   (UILabel *)[cell viewWithTag:300];
I tried the code, works for the name but not for the picture.

by the way, can explain why do i need to put "nameValue. tag = 200"?
eagleclaws is offline   Reply With Quote
Old 05-27-2011, 05:51 AM   #11 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by eagleclaws View Post
I tried the code, works for the name but not for the picture.

by the way, can explain why do i need to put "nameValue. tag = 200"?
work for the label and not for the image, because i haven't touched the code part for image, thinking that you would be able to do it after seen how to do for labels

I think that you miss how work the code you wrote, what happen is that when you scroll uitableview, cellForRowAtIndexPath is called for each new cell that you see, in that we use dequeueReusableCellWithIdentifier that try to re-use an old cell (this for performance reason), if so, you need to remove\change the old label and the old image (as you seen i removed it, but you can also just change its value). In order to have a reference to the old image or label, we use a tag, so that we can use viewWithTag:tag to retrieve a pointer to the view to remove\change (in this case 2 labels and image).

I hope now is clear, let me know if you are able to understand and make it works also for image....
__________________

Last edited by dany_dev; 05-27-2011 at 05:55 AM.
dany_dev is offline   Reply With Quote
Old 05-27-2011, 11:47 AM   #12 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 56
eagleclaws is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
work for the label and not for the image, because i haven't touched the code part for image, thinking that you would be able to do it after seen how to do for labels

I think that you miss how work the code you wrote, what happen is that when you scroll uitableview, cellForRowAtIndexPath is called for each new cell that you see, in that we use dequeueReusableCellWithIdentifier that try to re-use an old cell (this for performance reason), if so, you need to remove\change the old label and the old image (as you seen i removed it, but you can also just change its value). In order to have a reference to the old image or label, we use a tag, so that we can use viewWithTag:tag to retrieve a pointer to the view to remove\change (in this case 2 labels and image).

I hope now is clear, let me know if you are able to understand and make it works also for image....
thanks for the explanation, i will try it on monday and see how it goes
eagleclaws is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 347
6 members and 341 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0