【发布时间】:2012-08-21 10:14:19
【问题描述】:
我正在使用启用了 ARC 的 Xcode 4.4.1 和情节提要(以防这会有所不同)
我有一个 UITableViewController,里面有一个表格视图(表格视图使用“副标题”单元格)
我正在使用 NSArray 来填充我的表格:
@property (strong, nonatomic) NSArray *myData;
我在viewDidLoad中得到这个表中的数据
- (void)viewDidLoad
{
[super viewDidLoad];
self.myData = [self.myCalendarModel GetWeightHistory] ;
}
然后我有:numberOfSectionsInTableView 和 numberOfRowsInSection
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myData count];
}
最后是我的 cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WeighDataCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
WeightHistory *myDataForCell = [self.myData objectAtIndex:indexPath.row];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MM.YYYY"];
NSString *dateString = [dateFormat stringFromDate:myDataForCell.weightDate];
cell.textLabel.text = dateString;
cell.detailTextLabel.text = [myDataForCell.weight description];
return cell;
}
我可以毫无问题地展示我的表格。我有空间显示 6 个单元格,我的 NSArray 有 6 条记录。当我在我的桌子上向下滚动时,我没有任何问题。
当我向上滚动时,如果没有单元格离开视图,我没有任何问题。当我松开手指时,只要一个单元格不在视野范围内,就会出现 Exc_bad_access 错误。
使用 NSZombieEnable 进行调试时,我可以看到:
[CalendarHistoryTableViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x6eaf6f0
所以我猜我的手机被释放了,这就是我遇到这个问题的原因。但我不知道何时以及如何防止这种情况。
感谢您提供的任何帮助! 埃里克
@FaddishWorm 是的,标识符已设置,如果单元格不为零,则表示已分配。但这部分似乎很有效,因为我可以将我的数据显示在屏幕上。
@Pandey_Laxman 感谢您的评论。这几乎是我在这个类中唯一的代码。要检查问题是否与我的 WeightHistory 对象无关,我已从代码中删除了这部分,但我仍然遇到相同的错误
这就是我的新 cellForRowAtIndexPath 的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WeighDataCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = @"test";
cell.detailTextLabel.text = @"test2";
return cell;
}
【问题讨论】:
-
您是否在情节提要中为“WeighDataCell”设置了单元格的标识符?
-
另外,你不需要分配你的单元格吗?只有在 cell==nil 时才分配它
-
您上面的代码没问题,请检查与CalendarHistoryTableViewController相关的其他代码行
-
感谢您的 cmets 我已经编辑了我的原始帖子并提供了更多信息。
标签: iphone ios ios5 uitableview exc-bad-access