views:

209

answers:

3

Hello.

I've seen a number of people having a similar issue, but either their solution did not help, or it was too different.

My problem is, I have a customized UITableViewCell, custom size, image and content. When I scroll up or down and then back again, the text within some of the cells disappears. This seems to be happening randomly.

I have read about the "dequeue"-issue but either I got it wrong or it doesnt fall into my case....

Anyways, heres the code for my Cells:

- (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];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;


cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:
                        [[NSBundle mainBundle]  pathForResource:
                         [[shopItems  objectAtIndex:indexPath.row] name] ofType:@"jpg"]];

UITextField *temp= [[UITextField alloc] initWithFrame:cell.frame];
temp.text = [[shopItems objectAtIndex:indexPath.row] name];
temp.textColor = [UIColor whiteColor];
temp.textAlignment = UITextAlignmentCenter; 


UIImageView *cellView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
                                                                   [[NSBundle mainBundle] pathForResource:@"smalltabs_wide_bg"  ofType:@"png"]]];

[cellView addSubview:temp];

cell.backgroundView = cellView;



return cell;
}

Do you have any(!) ideas on this?

A: 

One problem is that you are not releasing your temp UITextField, this will lead to a memory leak and could be causing the problems.

cory.m.smith
true, I forgot to release it, but now that I fixed that, the problem remains.
Icky
+1  A: 

The possible problem is that you add UITextField and UIImageView to your cell each time you (re-)use the cell. Your code should look like

 {
   ...
   if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];

   // Insert all subviews to cell here
   }

   // setup cell's subviews here
   ...
   }

And also mind all memory leaks in your code - when you create something with alloc, you must release it somewhere.

Vladimir
That was it, I guess I should read about that guide more properly. Thanks for all the advice - I also fixed that memory thing.
Icky
A: 

use reusableCell method

read this discussion

http://discussions.apple.com/thread.jspa?threadID=2075838&tstart=1035

Ankit Sachan