When uitableview is scrolled the expanded cells are shuffled and whole table cells are shuffled and overlapped. Why this happens and how can i set this.Please guide, thanks in advance.
Here is the code:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
if(tableView == self.mTableView)
{
cell = [self.mTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
if(tableView == self.mMenuTableView)
{
cell = [self.mMenuTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.section == 0)
{
if(!isShowingList)
{
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"event_exp.png"]];
}
else
{
if(indexPath.row == 0)
{
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"event_exp_active.png"]];
}
if(indexPath.row == 1)
{
cell.textLabel.text = [self.mArrSubMenuEvent objectAtIndex:0];
cell.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:218.0/255.0 blue:250.0/255.0 alpha:1.0];
}
if(indexPath.row == 2)
{
cell.textLabel.text = [self.mArrSubMenuEvent objectAtIndex:1];
}
if(indexPath.row == 3)
{
cell.textLabel.text = [self.mArrSubMenuEvent objectAtIndex:2];
}
}
}
if(indexPath.section == 1)
{
if(!isOverhead)
{
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"overhead_exp.png"]];
}
else
{
if(indexPath.row == 0)
{
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"overhead_exp_active.png"]];
}
if(indexPath.row == 1)
{
cell.textLabel.text = #"Friend";
}
if(indexPath.row == 2)
{
cell.textLabel.text = #"Public";
}
}
}
if(indexPath.section == 2)
{
NSString *imgName = [self.mArrCellImages objectAtIndex:indexPath.row];
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Set reuseIdentifier as nil.
cell = [self.mTableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
Related
I am doing one registration form for iphone application. In that am having one tableview with four section.In last section am having one row with register button.I got the tableview with data well.But while scrolling the tableview up and down my register button goes to 2nd section.I added the code below.What wrong i did in this.Help me to solve.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *identifier = #"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSLog(#" row and section ===== %d,%d",indexPath.row,indexPath.section);
if(indexPath.section != 3)
{
UILabel *lbl_title = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(7,10,250,50):CGRectMake(70,40,340, 35))];
[lbl_title setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_title.backgroundColor = [UIColor clearColor];
[lbl_title setTextColor:[UIColor blackColor]];
lbl_title.tag = ((indexPath.section+1)*10)+indexPath.row;
//NSLog(#"lbl data tag %d",lbl_title.tag);
lbl_title.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:lbl_title];
UILabel *lbl_data = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(260,10,410,50):CGRectMake(70,40,340, 35))];
[lbl_data setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_data.backgroundColor = [UIColor clearColor];//25-25-112
[lbl_data setTextColor:[UIColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:112.0/255.0 alpha:1.0]];
lbl_data.textAlignment = UITextAlignmentCenter;
lbl_data.tag = ((indexPath.section+1)*100)+indexPath.row;
//NSLog(#"lbl data tag %d,%#",lbl_data.tag,lbl_data);
[cell.contentView addSubview:lbl_data];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
lbl_title.text = #"Email";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Password";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Confirm password";
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
lbl_title.text = #"First name";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Last name";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Nickname";
}
else if(indexPath.row == 3)
{
lbl_title.text = #"Gender";
}
}
else if(indexPath.section == 2)
{
if(indexPath.row == 0)
{
lbl_title.text = #"City";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"State";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Country";
}
}
}
if(indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.backgroundColor = [UIColor colorWithRed:165.0/255.0 green:42.0/255.0 blue:42.0/255.0 alpha:1.0];//165-42-42
UIButton *btn_register = [[UIButton alloc] initWithFrame:isiPad?CGRectMake(10,0,660,70):CGRectMake(0,0,320,40)];
[btn_register addTarget:self action:#selector(btn_register_clicked:) forControlEvents:UIControlEventTouchUpInside];
[btn_register setTitle:#"Register" forState:UIControlStateNormal];
//btn_register.titleLabel.textAlignment = UITextAlignmentCenter;
btn_register.backgroundColor = [UIColor blueColor];
[btn_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn_register setFont:[UIFont fontWithName:#"Verdana-Bold" size:24]];
[cell.contentView addSubview:btn_register];
}
}
}
else
{
NSLog(#"updating cell.........");
}
return cell;
}
It gets repeated because you added it to a cell's contentview, and then you re-use that cell.
Rather than adding it in cell's contentView, add the button as a cell accessory.
cell.accessoryView = btn_register;
And in all other cells, set accessoryView to nil.
cell.accessoryView = nil;
Why don't you try setting different identifiers for each section of cells. You can also use Use [tableView dequeueReusableCellWithIdentifier:forIndexPath:
Using a different cell identifier for the "Register" cell should work. And your code will be much clear if you use a container to hold the data, like an NSDictionary. For example:
NSArray *account = #[#"Email", #"Password", #"Confirm password"];
NSArray *userInfo = #[#"First name", #"Last name", #"Gender", #"Nickname"];
// ...
NSDictionary *data = #{#"Account": account, #"User Information": userInfo /* ... */};
i have a little social network and i tried to implement the ratings code into the table view, it judges ratings with stars... the problem is that the second section displays ratings from the first table section
picture:
so you can see it displays ratings in some of the tableview cells, just the last couple, why would it be doing this?
drawing code:
-(void)drawRect:(CGRect)rect
{
if(_shouldDrawRating)
{
self.desc_image.layer.cornerRadius = 8;
self.desc_image.clipsToBounds = YES;
_rating = [[UIStarRateView alloc]initWithFrame:CGRectMake(220,35,70,15)];
[_rating setMaxStars:5];
[_rating setFullImage:[UIImage imageNamed:#"StarFull.png"]];
[_rating setHalfImage:[UIImage imageNamed:#"StarHalf.png"]];
[_rating setEmptyImage:[UIImage imageNamed:#"StarEmpty.png"]];
[_rating setRate:_postRating];
[self addSubview:_rating];
}
}
cell rendering code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Regular_Cell *cell;
// Configure the cell...
if(indexPath.section == 0)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"default"];
}
NSDictionary *dict = self.recentArray[indexPath.row];
[cell setShouldDrawRating:YES];
[cell.title_label setText:dict[#"title"]];;
[cell.author_label setText:[NSString stringWithFormat:#"Author: %#",dict[#"author"]]];
[cell.views_label setText:[NSString stringWithFormat:#"Views: %#",dict[#"views"]]];
[cell setPoll_idFromString:[NSString stringWithFormat:#"%#",dict[#"id"]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",dict[#"imageURL"]]];
[cell.desc_image setImageURL:imageURL];
[cell setPostRating:indexPath.row+0.5];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"See More"];
}
}
else
{
cell = nil;
}
NSLog(#"%#",indexPath);
}
if(indexPath.section == 1)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"default"];
}
NSDictionary *dict = self.topViewedArray[indexPath.row];
[cell.title_label setText:dict[#"title"]];
[cell.author_label setText:[NSString stringWithFormat:#"Author: %#",dict[#"author"]]];
[cell.views_label setText:[NSString stringWithFormat:#"Views: %#",dict[#"views"]]];
[cell setPoll_idFromString:[NSString stringWithFormat:#"%#",dict[#"id"]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",dict[#"imageURL"]]];
[cell.desc_image setImageURL:imageURL];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"See More"];
}
}
else
{
cell = nil;
}
NSLog(#"%#",indexPath);
}
return cell;
}
In section 1 you have to set the boolean to NO,It doesnt have a proper value now in section 1.I think you miss this line.
[cell setShouldDrawRating:NO];
Not only in Section 1 where ever you create the cell it must be implemented,Otherwise can cause problem of inconsistency in drawing
Replace code for setting cell labels, images etc from tableView:cellForRowAtIndexPath: and paste it in tableView:willDisplayCell:forRowAtIndexPath:. And setShouldDrawRating: in each section, not only in section number 0.
I know that if I have some images and subviews added in customized cell then I have to reuse the cell so that custom control won't appear on other cells but here I have other issue. I just want to have ImageView on first cell of first section so I have used IndexPath.Section==0 and IndexPath.Row==0 condition in following code but the problem is when I scroll table, the other cell will meet this condition and my code will create imageview on that cell as well. I have tried Tagging it and using same tagged cellView but it didn't help either. The cell issue is with disabling user interactions for few cells. Eventually after scrolling it disables user interactions for all cells. Is there anyway to resolve this?
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
[imageView setFrame: CGRectMake(10, 10, 54, 54)];
[cellView addSubview:imageView];
cell.backgroundView = cellView;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;
return cell;
}
cell.textLabel.text = #"Test";
return cell;
}
[RESOLVED] Correct code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
cell.textLabel.text = nil;
cell.textLabel.textColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.imageView.image = nil;
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = NO;
return cell;
}
cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}
[IMPROVED CODE]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NormalCellIdentifier = #"NormalCell";
static NSString *TitleCellIdentifier = #"TitleCell";
NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) {
neededCellType = TitleCellIdentifier;
} else {
neededCellType = NormalCellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new
if([neededCellType isEqualToString: TitleCellIdentifier]) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
}
}
if([neededCellType isEqualToString: NormalCellIdentifier]) {
NSString * title;
if(indexPath.row == 0) {
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.text = #"Test";
}
}
return cell;
}
I think your problem is that the reuse of cells makes it so that the cells that aren't being created as new cells have some properties set that you must redefine. For instance, try assigning cell.userInteractionEnabled = YES to all other cases and see what the result is.
The problem is that you are not allowing for the possibility that the cell that was correctly showing the image gets reused later and the image view is still in there.
Here are two solutions:
set the tag value of the image view when you create it, then when you setup the cells, include code to check for and remove the old imageView if necessary.
assign different reuse identifiers to cells that need an image view and those that do not. Then make sure that you are only adding a new image view to cells when they are being created and not when they are being reused.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NormalCellIdentifier = #"NormalCell";
static NSString *TitleCellIdentifier = #"TitleCell";
NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) {
neededCellType = TitleCellIdentifier;
} else {
neededCellType = NormalCellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new
if([neededCellType isEqualToString: TitleCellIdentifier]) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
[imageView setFrame: CGRectMake(10, 10, 54, 54)];
[cellView addSubview:imageView];
cell.backgroundView = cellView;
}
}
if([neededCellType isEqualToString: NormalCellIdentifier]) {
NSString * title;
if(indexPath.row == 0) {
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;
}
else {
cell.textLabel.text = #"Test";
return cell;
}
}
}
(Those last few lines fell out of the code box). That should do it.
I have a settings view with 3 sections. Some cells have different styles: Default or Value1. When I swipe fast up or down, or change view and come back, the text supposed to be in a cell (for example the detailTextLabel in my cell with StyleValue1) is either not here anymore, or sometimes in a cell above or below... Here is the screenshots: the first is the normal state, the second the detailTextLabel from Version went to the cell above, and in the third the Measurement System detailTextLabel disappeared...
And here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (indexPath.section == 1 && indexPath.row == 0) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else if (indexPath.section == 2 && indexPath.row == 2) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
}
// Selection style.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// Vehicles cells.
if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count]) {
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.text = [[NSString stringWithFormat:#"%# %# %#",
[[self.userCarsArray objectAtIndex:indexPath.row] year],
[[self.userCarsArray objectAtIndex:indexPath.row] make],
[[self.userCarsArray objectAtIndex:indexPath.row] model]] uppercaseString];
// Checkmark if current car.
if ([[EcoAppAppDelegate userCar] idCar] == [[self.userCarsArray objectAtIndex:indexPath.row] idCar]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
selectedCarPath = indexPath;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
// Add car cell.
if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count]) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = #"Add Vehicle";
}
// General cells.
if (indexPath.section == 1 && indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Measurement System";
cell.textLabel.textColor = [UIColor darkGrayColor];
if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
cell.detailTextLabel.text = #"Miles";
else
cell.detailTextLabel.text = #"Meters";
}
// Information cells.
if (indexPath.section == 2 && indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"About";
cell.textLabel.textColor = [UIColor darkGrayColor];
}
if (indexPath.section == 2 && indexPath.row == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"License";
cell.textLabel.textColor = [UIColor darkGrayColor];
}
if (indexPath.section == 2 && indexPath.row == 2) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"Version";
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
Do you know how I can fix this issue? Thanks!
All your cells use the same reuseIdentifier, so when you scroll, you get an old cell and you set texts on it
You can solve your problem by setting
cell.detailTextLabel.text
for all cases
When using reuseIdentifier, you should set every time all fields content that change
I am creating my cells from an array. The last object in my area gets created differently, as I want to add a new UILabel to the contentView of the cell. (So that it shows a total number of records in a UILabel in the last cell).
For some reason, my last cell that adds the UILabel to the contentView keeps getting duplicated. It will show up on other cells, screw up their display, etc. I have a feeling this has to do with cell reuse, but I am not sure how to fix it.
Here is my method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"cellID";
// Bounds
CGRect bounds = [[UIScreen mainScreen] bounds];
Person *person = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
person = [people objectAtIndex:indexPath.row];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Check if it is the first cell
if (person == [people lastObject]) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.userInteractionEnabled = NO;
UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(-4, 11, bounds.size.width - 10, 20)];
count.autoresizingMask = UIViewAutoresizingFlexibleWidth;
count.textColor = [UIColor darkGrayColor];
count.font = [UIFont systemFontOfSize:16];
count.textAlignment = UITextAlignmentCenter;
count.text = person.nameFirst;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
[cell.contentView addSubview:count];
return cell;
}
}//end
cell.textLabel.text = [NSString stringWithFormat:#"%#, %#", person.nameLast, person.nameFirst];
// Check if they are faculty staff
if ([person.status isEqualToString:#"Staff/Faculty"]) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#", person.status, person.department];
} else {
cell.detailTextLabel.text = person.status;
}//end
return cell;
}
Can someone help me understand how I can get this working correctly so that my UILabel doesn't get created on different cells?
I have adjusted your method below. Basically, you need to dequeue cells and perform any cell wide settings (disclosure, etc.) when you create the cell. Any individual cell changes should be done after the cell has been dequeued.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"cellID";
static NSString *lastCellID = #"lastcellID";
// Bounds
CGRect bounds = [[UIScreen mainScreen] bounds];
Person *person = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
person = [people objectAtIndex:indexPath.row];
}
UITableViewCell *cell;
if (person != [people lastObject]) {
cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [NSString stringWithFormat:#"%#, %#", person.nameLast, person.nameFirst];
// Check if they are faculty staff
if ([person.status isEqualToString:#"Staff/Faculty"]) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#: %#", person.status, person.department];
} else {
cell.detailTextLabel.text = person.status;
}//end
} else {
cell = [tableView dequeueReusableCellWithIdentifier:lastCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lastCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.userInteractionEnabled = NO;
UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(-4, 11, bounds.size.width - 10, 20)];
count.autoresizingMask = UIViewAutoresizingFlexibleWidth;
count.textColor = [UIColor darkGrayColor];
count.font = [UIFont systemFontOfSize:16];
count.textAlignment = UITextAlignmentCenter;
count.tag = 1;
[cell.contentView addSubview:count];
}
UILabel *count = (UILabel *)[cell viewWithTag:1];
count.text = person.nameFirst;
//cell.textLabel.text = nil;
//cell.detailTextLabel.text = nil;
}//end
return cell;
}
It's probably because the cell will be reused. Use a different reuse identifier for the last cell, say, kLastCellID.
UITableViewCell *cell = nil;
// Check if it is the first cell
if (person == [people lastObject]) {
cell = [tableView dequeueReusableCellWithIdentifier:kLastCellID];
if (!cell) {
//create a last cell, with an UILabel in your content view
}
//update the cell's content
} else {
cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (!cell) {
//create a regular cell
}
//update the cell's content
}