04-02-2010, 01:46 AM
#1 (permalink )
Registered Member
Join Date: Feb 2010
Posts: 6
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;
}
04-02-2010, 02:14 AM
#2 (permalink )
Registered Member
Join Date: Sep 2009
Posts: 1,018
you're using [lbl setText:] only in the first of the two if cases
04-13-2010, 07:53 AM
#3 (permalink )
Registered Member
Join Date: Jul 2009
Location: New Delhi
Posts: 130
Quote:
Originally Posted by
sandyshankar
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
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,872
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031