我有这个UITableView应用程序,一个基本的日记应用程序,在这个方法XCode说我有内存泄漏。建议泄漏的第一行是117“NSString * CellIdentifier”。 if to if(cell == ...,to Diary * diaryEntry,to NSString * stringDate。 在那里它声明该方法返回一个具有+1保留计数的对象,拥有。 我试图释放cell,stringDate ......没有什么改变这个事实,所以我在想什么/做错了?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// A date formatter for the time stamp static
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Diary *diaryEntry = [diaryArray objectAtIndex:indexPath.row];
cell.textLabel.text = diaryEntry.diaryTitle;
NSString *stringDate = [[DiaryDateFormatter alloc] convertDateToString:[diaryEntry diaryDate]];
cell.detailTextLabel.text = stringDate;
[stringDate release];
return cell;
}
答案 0 :(得分:1)
这一行看起来有问题:
NSString *stringDate = [[DiaryDateFormatter alloc] convertDateToString:[diaryEntry diaryDate]];
convertDateToString:
是初始化方法,则应重命名。但是,看起来convertDateToString:
应该是一个类方法(这样你就不必分配DiaryDateFormatter
类的实例了。)alloc
方法始终为您提供+1保留计数的对象。因此,您有责任发布它。设置代码的方式意味着您无法再访问已分配的对象以释放它。尝试将上面的代码行更改为:
DiaryDateFormatter *formatter = [[DiaryDateFormatter alloc] init];
NSString *stringDate = [formatter convertDateToString:[diaryEntry diaryDate]];
[formatter release];
另外,不要发布stringDate
,因为它不是使用名称暗示您拥有所有权的方法获得的。
答案 1 :(得分:-1)
我看到了问题。你正在使用objective-c,你并不擅长它(像我一样!)
更重要的是,IIRC如果要返回它,你应该使用自动释放而不是为单元格分配?在我跳船之前,这就是我所记得的......