Customized UITableViewCell dissappears
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] pathFo开发者_运维知识库rResource:
[[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?
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.
use reusableCell method
read this discussion
http://discussions.apple.com/thread.jspa?threadID=2075838&tstart=1035
One problem is that you are not releasing your temp UITextField, this will lead to a memory leak and could be causing the problems.
Sharing my experience:
Had similar problems. I have many tables in the app, and in one of them data would randomly disappear, and eventually the entire table would go away.
The problem for me was that I was dequeueing cells, giving all of them the same "unique" id. So several tables were sharing the same id, and I believe they were conflicting and messing up the cells I was looking at.
Giving each table it's own unique identifier solved the problem for me. (dah!)
(posted this here too, might be a duplicate : UITableView custom cell images disappear after scrolling.)
精彩评论