表视图崩溃
我是iPhone应用程序开发新手。我目前正在研究XML解析。我从XML文件中提取记录并将其存储在可变数组中。我有一个表视图,我从该可变数组中加载表视图。我在调试中发现的表格视图中获得了18行。问题是,当我向下滚动到10行,它移动的很好,但第10行出现应用程序崩溃。我得到了EXC_BAD_ACCESS
的错误。这里是我的代码单元格被加载。谢谢。表视图崩溃
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
Location *record = [[Location alloc] init];
record = [[parse locationarr] objectAtIndex:indexPath.row];
cell.textLabel.text = record.locationname;
return cell;
}
}
你的错误是“恢复单元”这是写在里面,如果条件把它外面也行标题标签文本设置了侧condition.like这样..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 的UITableViewCell *电池= [tableView dequeueReusableCellWithIdentifier:@“cell”];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
Location *record = [[Location alloc] init];
}
else
{
// get record object here by tag.
}
record = [[parse locationarr] objectAtIndex:indexPath.row];
cell.textLabel.text = record.locationname;
return cell;
}
有在您发布一些代码misstakes,首先你只返回一个单元格是表中没有返回小区出队
从而改变你的代码也将解决这个问题:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
Location *record = [[Location alloc] init];
record = [[parse locationarr] objectAtIndex:indexPath.row];
cell.textLabel.text = record.locationname;
return cell;
}
也不必每次都alloc
和init
一个Location
是非常昂贵的,最好在您致电每次需要单元格一次,学生们有一个Location
用。 Beter现在只是为单元格获取数据,而不是在tableVieew:cellForRowAtIndexPath:
方法中执行任何冗长的方法。
问题就迎刃而解了。在if条件之外写了这段代码,它完美地工作。感谢大家。 :) – Paul 2013-03-25 12:42:29
你需要写这个片段
`record = [[parse locationarr] objectAtIndex:indexPath.row];
cell.textLabel.text = record.locationname;`
出来,如果块。
享受编程!
没有真正的,因为他仍然只是在if语句中返回一个单元格。 – rckoenes 2013-03-25 11:02:16
是的,我没有注意到!然后,他需要返回单元格也阻止! – 2013-03-25 11:03:27
展示一些你的崩溃日志... – BhushanVU 2013-03-25 10:39:30