Reusable UITableViewCells repeating content - objective-c

I'm having a very common problem that I don't know how to fix for myself, even after reading a bunch of similar posts, since making my cells non-reusable is not an option (the app slows down immensely).
The Problem
When I have 6 or more cells (maximum of 16), the contents of the first 5 cells begin appearing in all of the other cells when I scroll to them.
Though I am using reusable cells, why are the contents of my cells not being refreshed with new data that's stored in my NSMutableArrays?
The Code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WorldClockTableViewCell *worldClockCell = (WorldClockTableViewCell*)[tableView dequeueReusableCellWithIdentifier:kCellID];
if(worldClockCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WorldClockTableViewCell" owner:self options:nil];
worldClockCell = [topLevelObjects objectAtIndex:0];
/*
Set Scrolling to OFF by default.
User can scroll when table is in edit mode.
*/
[worldClockCell.scrollView setScrollEnabled:NO];
// Set Digital Clock in timeLabel
[self setDigitalClockInLabel:worldClockCell inRow:indexPath.row];
// Set Analog Clock Carousel in ScrollView in every cell.
[self setAnalogClocksInScrollView:worldClockCell inRow:indexPath.row];
// Scroll to User's Preferred Clock (if this is the first launch of WorldClockTableViewController)
if([[shouldAutoScrollArray objectAtIndex:indexPath.row] boolValue] == YES) {
[self scrollToUsersPreferredClock:worldClockCell atRow:indexPath.row];
[shouldAutoScrollArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
}
// Add City Name to cityLabel in every cell
[worldClockCell buildFormattedCityName:[model.timeZoneCityNames objectAtIndex:indexPath.row]];
// Make sure new clocks created while in 'edit' mode don't scroll out of the cell
worldClockCell.scrollView.frame = worldClockCell.scrollViewFrame;
}
return worldClockCell;
}

You're doing all your cell configuration only if it created a new cell, not if it dequeued one. Pull everything out of
if(worldClockCell == nil)
except
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WorldClockTableViewCell" owner:self options:nil];
worldClockCell = [topLevelObjects objectAtIndex:0];
unless you really only need to do it once, when the cell is created (and not every time it's reconfigured to display new data).

You should move all the digital and analog clocks out of the scope of check for dequeued reusable cell and all will be good. Your content is being repeated because the cell once scrolled out of view is kept for reuse.

Related

How to avoid selecting other cells that have the same indexPath

I've been struggling with this issue for weeks and still have not found one solution. No one really gave me an answer on SO and have not found anything that helps my issue either.
When making a change of a cell's content's color, position, whatever, other cells with the same indexPath also get modified.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
static NSString *simpleTableIdentifier = #"cell";
self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
self.cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
This is how I create cells. Help..!
EDIT
This is a slide-out view implemented in each cell. When you swipe to right on a cell, it shows up from the left but even though only the selected cell's content should be modified, several other cell's slide-out menu also come out.
This is how I implement slide-out menu in my custom cell class.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.optionView = [[OptionView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width , self.contentView.frame.size.width)];
self.optionView.delegate = self;
[self.contentView addSubview: self.optionView];
OptionView' delegate - called when swiped to the right
-(void)handleGesture:(UISwipeGestureRecognizer*)shouldOpenMenu{
POPSpringAnimation *anim = [POPSpringAnimation animation];
anim.property = [POPAnimatableProperty propertyWithName:kPOPViewFrame];
anim.toValue = [NSValue valueWithCGRect:CGRectMake(-self.frame.size.width/4, 0, self.optionView0.frame.size.width, self.optionView0.frame.size.height)];
anim.springBounciness = 11;
anim.springSpeed = 5;
[self.optionView0.layer pop_addAnimation:anim forKey:#"spring0"];
First, each tableview cell has different index path.
Second, you never want to save cells at class level properties. Create them locally and return from cellForIndexPath:. Your table view will take care of the cell thereon.
Third, and very important, cells are reused. So, you should reset the cell before using it. For instance, if you are setting cell colour to red in some condition and then you move the table down and this cell gets discarded and new cell is rendered by re-using the discarded cell. You would want to reset the cell colour to clear if you are not setting cell colour for that row.
Follow these rules and you should be able to fix your issue.

reloading a uitableview starts at the 2nd row (or not at the first row)

Uitableview problem again. Whenever I reload my data, the tableview would reload but it seems like the data will not start at the first row. Please refer to the image below.
I tried the following after the reload of data but still no success:
[self.tableView reloadData];
// this or the other one ... [self.tableView setContentOffset:CGPointZero animated:NO];
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
This is how I positioned the uitableview on viewdidload
self.tableView.frame = CGRectMake(450, 20, self.tableView.frame.size.width, self.tableView.frame.size.height - 20);
EDIT 1:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SearchResultCell";
User *user = [users objectAtIndex:indexPath.row];
UserTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UserTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.name.text = [NSString stringWithFormat:#"%# %#", user.firstName, user.lastName];
return cell;
}
Based on your comments, your table is being resized incorrectly when you are rotating the device to landscape mode. In your storyboard, you need to set the autoresizing on the table view in order for it to properly resize the way that you want.
Here is a good starting point for something like this:
Notice just above the Autosizing label, it shows a bunch of red bars (called springs and struts). I have them all turned on, which will maintain the distance from the edge of the superview and allow the object to change its' size in order to keep those distances the same. This should be close to what you want, and if not then you can play around with the different combinations until you get what you want.
You turn each part of it on and off by clicking on the red line.
For more details, take a look at this question: Autoresizing masks programmatically vs Interface Builder / xib / nib

SetNeedsDisplay in table view

My problem is that not all cell are correct initialized with the correct UIView as an rendered image.( see code below) On a iphone 4 it always the same cel on a iphone with retina display is also an other cell. In this set-up setNeedsDisplay won't function.
If i use the same structure in an IBoutlet it's working.
!
I need to use in some cells the image-files .png and at some other cells the defined drawing method, which uses the drawrect or better i should use the setNeedsDisplay method.....
What is going wrong!!!
my code
tableview... cell
static NSString *CellIdentifier = #"TypeCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
CellTypes *cellType = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = cellType.title;
cell.detailTextLabel.text = cellType.detail;
if ( [cellType.type rangeOfString:#"TL"].location != NSNotFound) {
cell.imageView.image = [UIImage imageNamed:[cellType.type stringByAppendingString:#"Thumb"]];
cell.indentationWidth = 10;
}
else {
static CGFloat scale = 0.0; // old API , screen values
UIScreen *screen = [UIScreen mainScreen];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
scale = [screen scale];
}
if (scale>0.0) {
UIGraphicsBeginImageContextWithOptions(cell.imageView.bounds.size, NO, scale);
}
else {
UIGraphicsBeginImageContext(cell.imageView.bounds.size);
}
CGRect imageRect = CGRectMake(0, 0, 84, 84 ); // cell.imageView.bounds;
FittingImageView *fittingImage = [[FittingImageView alloc] initWithFrame:imageRect];
fittingImage.thumb = YES;
fittingImage.title = offSetType.title;
[fittingImage drawRect:imageRect]; // this works but skips one imageRect
// [fittingImage setNeedsDisplay]; //this won't work.
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return cell;
You can never call drawRect: yourself. It is called by Cocoa, and only Cocoa can setup the context. It's unclear what you're trying to do here. I don't know what a FittingImageView is. You're creating it and then throwing it away in any case.
You then try to render the cell itself into an image, and the put that image into the cell's imageview. That doesn't make sense.
You may misunderstand how tableview cells are created, reused and drawn. You should re-read "Creating and Configuring a Table View."
The primary things to remember:
In this routine, your job is to either create a new cell, or reconfigure a reusable cell (if dequeueReusableCellWithIdentifier: returns you something).
This routine is not for drawing a cell. That will happen much later, and it is the cell's job to draw itself. You're just configuring the cell. Putting everything in it that it needs to draw later.
You typically do not want to compute expensive things in this routine. Compute those elsewhere and cache them. This routine should configure a cell, slap data into it, and return. If you try to create new images in this routine, your table view is going to stutter.
You shouldn't mess with the cell's layer in the middle of this routine. If you need something that complex, you should be creating a custom cell with its own drawRect:.

How to make a-two-column UITableViewController list?

I need to present my table view with a two column list. I read some related posts about making some grid by overriding drawRect (here). However, I'm seeking a simple way to design my cell with IB in nib, then load it and push two cells on each row. The example with drawRect is not appropriate because it involves manual setting of positions. I just need to push those two cells with some autosizing, that's it. Is it possible?
I'm looking for something like (in cellForRowAtIndexPath):
cell.contentView = emptyUIViewContainer;
[cell.contentView addSubview:FirstColumnUIView];
[cell.contentView addSubview:SecondColumnUIView];
I don't need to have two separate nibs for those two columns because every column is of the same format just with some other data. Any ideas?
UPDATE: Intuitively, I'm trying to do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the first cell.
cell1.textLabel.text = ...some text
// Configure the second cell
UITableViewCell *cell2 = [[UITableViewCell alloc] init];
cell2.textLabel.text = ...some text
//set row as container for two cells
UITableViewCell *twoColumnRowView = [[UIView alloc] init]; //initWithFrame:CGRectMake(0, 0, 200, 20)];
cell1.contentView.frame = CGRectMake(0, 0, 100, 20);
[twoColumnRowView addSubview:cell1];
cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
It's just a raw prototype I'm playing with now. But the code crashes at runtime with "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance"
UPDATE 2. I have changed a code to look more practical. Strange but after several tries I got the app working without crash but with weird black background in all cells. Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstCellIdentifier = #"FirstCellIdentifier";
static NSString *SecondCellIdentifier = #"SecondCellIdentifier";
// initialize first cell
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
}
//initialize second cell
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];
if (cell2 == nil) {
cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
}
cell1.textLabel.text = ...data
cell2.textLabel.text = ...data
UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];
[twoColumnRowView addSubview:cell1];
//cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
I'm not reusing twoColumnRowView but both other does do.
You can't (or at least, shouldn't) put two table views next to each other. And, as you already know, a table view only ever has a single column.
However, you can put as much data as you want in each cell.
You can do most of this in Interface Builder. Create a XIB with a UITableViewCell. Drag and drop a couple of UILabels into the right place. You could update the labels by finding them with the viewWithTag: method, but it would be better to create a custom UITableViewCell class with properties pointing to each label.
Since tables are so central to UIKit, there are a lot of samples in Apple's documentation set and some good WWDC talks. You can download the videos from iTunes.
Hope this will help you...
http://www.iphonedevx.com/?p=153
else the best way is to put two table views side by side, if they need to scroll independently.
Hope this will help you...

UITableView subview appears in wrong section when scrolling?

In a UITableView I add a UIView as subview but ONLY for section 1. Section 1's content is loaded from a plist and the plist contains mutable content. If there are enough rows to allow scrolling, then the following happens: I scroll to the bottom, and back up, and the UITextField appears randomly on some of section 0's cells. I have no clue why this is happening! So what i do is this (in ´cellForRowAtIndexPath´):
if (indexPath.section == 0) {
//do stuff
}
else if (indexPath.section == 1) {
d = [UIView alloc] init];
[cell.contentView addSubview:d];
}
and this gets totally messed up when I scroll. The subviews appear in section 0 where they shoudnt, and on didSelectRowAtIdexPath I reload for section 1 and then subviews even appear twice (over each other)... Its a complete MESS! Please, Please help.......
Without seeing any code this seems to be an issue pertaining to reusable cells. What happens is that the cells that have scrolled off the screen are reused for the new content that is to be shown. So i reckon you need to make a distinction in cellForRowAtIndexPath for section 0 and 1 and basically use different set of cells for them.
EDIT: Ok ima give a shot to your problem here
UITableViewCell *cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithoutSubview"];
if (cell ==nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"CellWithoutSubview"] autorelease];
}
//do stuff with cell like set text or whatever
}
else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:#"CellWithSubview"];
if (cell ==nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"CellWithSubview"] autorelease];
d = [[UIView alloc] init];
[cell.contentView addSubview:d];
[d release];
}
}
return cell;
So now you'll have two types of cells for the tableview that'll be reused one without the subview and one with the subview.
You must be using dequeueReusableCellWithIdentifier. The purpose of dequeueReusableCellWithIdentifier is to use less memory. If the screen can fit 4 or 5 table cells, then with reuse you only need to have 4 or 5 table cells allocated in memory even if the table has 1000 entries.
So the subviews in UITableViewCell are also cached. So when the cell is reused, you need to clean out the old view & then put in the new content.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: #"your-id"];
if (cell)
{
//reusing old cell. reset all subviews before putting new content.
}
else
{
//Create a fresh new cell
}
You should use switch instead:
switch ( indexPath.section )
{
case 0:
{
/* do soemthing */
}
break;
case 1:
{
d = [UIView alloc] init];
[cell.contentView addSubview:d];
}
break;
}