UITableCell textLabel is out of scope
I have a UITableViewcontroller that is reused for different data -- arrays of RecipeItems and ConvertedRecipeItems (which is a sub-class of RecipeItem).
The first time, I view ConvertedRecipeItems the cells render fine. If I view the detail of a cell and then go back, the SECOND ConvertedRecipeItem is crashing the app. When debugging, I'm seeing that the textLabel of the cell is "out of scope." I can't figure out what would be causing this.
Note that for regular RecipeItems this all works fine.
Here is the cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"RecipeCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//we're storing so don't auto-release
cell = [[[UITableViewCell alloc] initWithStyle开发者_StackOverflow中文版:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSUInteger row = [indexPath row];
RecipeItem* r = [recipes objectAtIndex:row];
cell.textLabel.text = r.name;
return cell;
}
Thanks!
You should autorelease the newly created cell; otherwise you're going to leak that cell.
What is the purpose of tl and tls? Remove those lines and try again. I have a feeling that those might be the culprit. And they're not doing anything anyway.
The out of scope error is interesting though—GDB usually doesn't report out of scope for properly declared ivars.
I can't see anything that may crash (providing you return correct count in tableView:numberOfRowsInSection:
). I suspect this issue has something to do with your details screen. What is console output on crash?
As Inspire48 already mentioned you should autorelease new cells. "//we're storing so don't auto-release" isn't correct. Table stores them, not instance of your class.
精彩评论