Nig*_*ury 0 memory-management objective-c ios4
我试图找出前3个小时的这个错误.通过搜索,我已经知道这个错误与内存管理有关,但我没有弄清楚我做错了什么.
我已经宣布了这4个标签:
@interface InteractiveHistory : UITableViewController {
UILabel *date;
UILabel *startTime;
UILabel *cal;
UILabel *duration;
}
Run Code Online (Sandbox Code Playgroud)
然后创建属性并合成它们.在viewDidLoad中,我初始化了所有这样的:
date = [[UILabel alloc] init];
Run Code Online (Sandbox Code Playgroud)
我也在dealloc()方法中释放它们.
我想要做的是使用这4个标签在表格的单元格中写入一些文本.文本将取决于indexPath.
在cellForRowAtIndexPath方法中,只显示2个标签并指示错误行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
NSLog(@"constructing cell");
//set up labels text
date = [NSString stringWithFormat:@"%@",[[int_data objectAtIndex:indexPath.row] objectForKey:@"Date"]];
NSLog(@"%@",date.text); //App crashes at this line
[date setTag:1];
startTime = [NSString stringWithFormat:@"%@",[[int_data objectAtIndex:indexPath.row] objectForKey:@"Start Time"]];
//setting tag and position of label
[startTime setTag:2];
CGRect labelpos = startTime.frame;
labelpos.origin.y = date.bounds.size.height + 2;
startTime.frame = labelpos;
NSLog(@"labels set");
// Configure the cell...
[[cell contentView] addSubview:date];
[[cell contentView] addSubview:startTime];
[[cell contentView] addSubview:duration];
[[cell contentView] addSubview:cal];
NSLog(@"A cell set");
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我,我做错了什么?我希望我的问题很清楚..
这条线:
date = [NSString stringWithFormat:@"%@",[[int_data objectAtIndex:indexPath.row] objectForKey:@"Date"]];
Run Code Online (Sandbox Code Playgroud)
应该:
date.text = [NSString stringWithFormat:@"%@",[[int_data objectAtIndex:indexPath.row] objectForKey:@"Date"]];
Run Code Online (Sandbox Code Playgroud)
否则,您的date指针将设置为NSString的实例,而不是设置其内容.
编辑:
正如danh正确指出的那样,你会遇到同样的问题startTime.
| 归档时间: |
|
| 查看次数: |
3631 次 |
| 最近记录: |