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 04-02-2010, 01:46 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 6
sandyshankar is on a distinguished road
Default UITableview scrolling issue

I am developing a navigation based application in which one view has a tableview. My application supports both portrait and landscape orientation.
I have enabled scrollbar for tableview. Previously i was not using the UITableViewCell in cellForRowAtIndexPath and it was causing overlapping of text of UILabel which is added as subview to table cell. Later i changed the code to reuse the tablecell and sub views.

Then i found new issue. When ever i scroll the table view, order of the cell is changed. sometime label of top cell will display at bottom and that of bottom at top. I dont have any idea till now how this is happening. I am reusing the cell and subviews. I have added the code of cellForRowAtIndexPath . Please let me know whats wrong in this.

As my application support both orientation, i call reloaddata for tableview in didRotateFromInterfaceOrientation and also from viewWillAppear to load table when view controller is popped.

I am using a transparent button and its touch down, touch up and touch up outside events for some animation and actions. I am not using didSelectRowAtIndexPath.

Please go through my code and please let me know whats wrong. I am currently out of ideas.

Code:
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";
	UILabel *lbl;
	UIButton *btnTrans;
	UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		btnTrans = [UIButton buttonWithType:UIButtonTypeCustom];
		btnTrans.tag = 1;
		[cell.contentView addSubview:btnTrans];
		if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
		{	
			lbl = [[UILabel alloc] initWithFrame:CGRectMake(90.0,4.0, 200.0, 37.0)];
		}
		else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
		{
			lbl = [[UILabel alloc] initWithFrame:CGRectMake(60.0,5.0, 200.0, 37.0)];
		}

		[cell.contentView addSubview:lbl];
		lbl.tag = 2;		
		lbl.backgroundColor = [UIColor clearColor];
		lbl.font = [UIFont systemFontOfSize:14];							
		lbl.textColor = [UIColor darkGreyColor];
		lbl.shadowColor = [UIColor whiteColor];					
		lbl.shadowOffset = CGSizeMake(0, 1.0);

		NSString *strText = [myArray objectAtIndex:indexPath.row];//My array has lot of objects		

		[lbl setText:strText];	
	}
	else
	{
		lbl = (UILabel*)[cell viewWithTag:2];
		btnTrans = (UIButton*)[cell viewWithTag:1];		
		if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
		{	
			[lbl setFrame:CGRectMake(90.0, 4.0, 200.0, 37.0)];
		}
		else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
		{
			[lbl setFrame:CGRectMake(60.0, 5.0, 200.0, 37.0)];
		}
	}

	UIImage *cellImage;
	UIImageView *imgCellView;			
	if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
	{
		imgCellView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 44.0)];
		cellImage = [UIImage imageNamed:@"curved-rectangle-h.png"];
		
		btnTrans.frame = CGRectMake(0.0, 0.0, 480.0, 44.0);
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateNormal];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateHighlighted];			
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateSelected];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateApplication];
	}
	else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
	{
		imgCellView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
		cellImage = [UIImage imageNamed:@"curved-rectangle-v.png"];
		
		btnTrans.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateNormal];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateHighlighted];			
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateSelected];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateApplication];
	}

	[btnTrans addTarget:self action:@selector(OnTouchUpOutSideCell:)  forControlEvents:UIControlEventTouchUpOutside];
	[btnTrans addTarget:self action:@selector(OnTouchUpCell:)  forControlEvents:UIControlEventTouchUpInside];
	[btnTrans addTarget:self action:@selector(OnTouchDownCell:)  forControlEvents:UIControlEventTouchDown];

	[imgCellView setImage:cellImage];
	[cell setBackgroundView:imgCellView];
	return cell;
}
sandyshankar is offline   Reply With Quote
Old 04-02-2010, 02:14 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1,018
Tambourin is on a distinguished road
Default

you're using [lbl setText:] only in the first of the two if cases
Tambourin is offline   Reply With Quote
Old 04-13-2010, 07:53 AM   #3 (permalink)
Registered Member
 
vikysaran's Avatar
 
Join Date: Jul 2009
Location: New Delhi
Posts: 130
vikysaran is on a distinguished road
Default

Quote:
Originally Posted by sandyshankar View Post
I am developing a navigation based application in which one view has a tableview. My application supports both portrait and landscape orientation.
I have enabled scrollbar for tableview. Previously i was not using the UITableViewCell in cellForRowAtIndexPath and it was causing overlapping of text of UILabel which is added as subview to table cell. Later i changed the code to reuse the tablecell and sub views.

Then i found new issue. When ever i scroll the table view, order of the cell is changed. sometime label of top cell will display at bottom and that of bottom at top. I dont have any idea till now how this is happening. I am reusing the cell and subviews. I have added the code of cellForRowAtIndexPath . Please let me know whats wrong in this.

As my application support both orientation, i call reloaddata for tableview in didRotateFromInterfaceOrientation and also from viewWillAppear to load table when view controller is popped.

I am using a transparent button and its touch down, touch up and touch up outside events for some animation and actions. I am not using didSelectRowAtIndexPath.

Please go through my code and please let me know whats wrong. I am currently out of ideas.

Code:
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";
	UILabel *lbl;
	UIButton *btnTrans;
	UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		btnTrans = [UIButton buttonWithType:UIButtonTypeCustom];
		btnTrans.tag = 1;
		[cell.contentView addSubview:btnTrans];
		if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
		{	
			lbl = [[UILabel alloc] initWithFrame:CGRectMake(90.0,4.0, 200.0, 37.0)];
		}
		else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
		{
			lbl = [[UILabel alloc] initWithFrame:CGRectMake(60.0,5.0, 200.0, 37.0)];
		}

		[cell.contentView addSubview:lbl];
		lbl.tag = 2;		
		lbl.backgroundColor = [UIColor clearColor];
		lbl.font = [UIFont systemFontOfSize:14];							
		lbl.textColor = [UIColor darkGreyColor];
		lbl.shadowColor = [UIColor whiteColor];					
		lbl.shadowOffset = CGSizeMake(0, 1.0);

		NSString *strText = [myArray objectAtIndex:indexPath.row];//My array has lot of objects		

		[lbl setText:strText];	
	}
	else
	{
		lbl = (UILabel*)[cell viewWithTag:2];
		btnTrans = (UIButton*)[cell viewWithTag:1];		
		if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
		{	
			[lbl setFrame:CGRectMake(90.0, 4.0, 200.0, 37.0)];
		}
		else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
		{
			[lbl setFrame:CGRectMake(60.0, 5.0, 200.0, 37.0)];
		}
	}

	UIImage *cellImage;
	UIImageView *imgCellView;			
	if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
	{
		imgCellView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 44.0)];
		cellImage = [UIImage imageNamed:@"curved-rectangle-h.png"];
		
		btnTrans.frame = CGRectMake(0.0, 0.0, 480.0, 44.0);
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateNormal];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateHighlighted];			
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateSelected];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_480_44.png"] forState:UIControlStateApplication];
	}
	else if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
	{
		imgCellView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
		cellImage = [UIImage imageNamed:@"curved-rectangle-v.png"];
		
		btnTrans.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateNormal];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateHighlighted];			
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateSelected];				
		[btnTrans setBackgroundImage:[UIImage imageNamed:@"trans_320_44.png"] forState:UIControlStateApplication];
	}

	[btnTrans addTarget:self action:@selector(OnTouchUpOutSideCell:)  forControlEvents:UIControlEventTouchUpOutside];
	[btnTrans addTarget:self action:@selector(OnTouchUpCell:)  forControlEvents:UIControlEventTouchUpInside];
	[btnTrans addTarget:self action:@selector(OnTouchDownCell:)  forControlEvents:UIControlEventTouchDown];

	[imgCellView setImage:cellImage];
	[cell setBackgroundView:imgCellView];
	return cell;
}
might work if you place
Code:
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
before if condition
__________________
Thanx & Regards-
Vaibhav Saran | Software Engineer
Neo Sypher Systems Pvt. Ltd.
New Delhi | India
Websites:
http://www.iphonesaura.com/
http://www.apptango.com/

vikysaran is offline   Reply With Quote
Reply

Bookmarks

Tags
scrolling, uitableview

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: 312
6 members and 306 guests
chemistry, heshiming, iAppDeveloper, jbro, kapps11, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,872
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

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