Iphone 滚动UITableview应用程序将崩溃?
在我的应用程序中,我使用的是Iphone 滚动UITableview应用程序将崩溃?,iphone,objective-c,uitableview,scroll,Iphone,Objective C,Uitableview,Scroll,在我的应用程序中,我使用的是UITableview。在该表视图中,我显示了一个包含字典的数组。TableView的最后一行包含一个按钮,指示用户加载更多事件。如果用户单击按钮,我们需要从服务获取数据并将数据加载到UITableview中 为此,我尝试按照以下代码实现它: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section** { if (ifMore) {
UITableview
。在该表视图中,我显示了一个包含字典的数组。TableView的最后一行包含一个按钮,指示用户加载更多事件。如果用户单击按钮,我们需要从服务获取数据并将数据加载到UITableview
中
为此,我尝试按照以下代码实现它:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section**
{
if (ifMore)
{
return [totalArray count];
}
else
{
return [tableArray count];
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int count =[tableArray count];
if (ifMore)
{
count= [totalArray count];
}
if (indexPath.row==count-1)
{
return 96;
}
return 56;
}
行索引方法的单元格为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
if (ifMore)
{
dict=[totalArray objectAtIndex:indexPath.row];
}
else
{
dict =[tableArray objectAtIndex:indexPath.row];
}
cell.hostLabel.text=[dict objectForKey:@"Event_Name"];
cell.startTime.text=[dict objectForKey:@"Event_Startdate"];
cell.endTime.text= [dict objectForKey:@"Event_Enddate"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
int count=[tableArray count];
if(ifMore)
{
count=[totalArray count];
}
if (indexPath.row==count-1)
{
[cell.contentView addSubview:moreBtn];
}
return cell;
}
我使用的代码是这样的。现在我的主要问题是,当我加载数据时,正常情况下它是好的,在加载滚动时不会产生任何问题。它工作得很好。但当我单击TableView单元格中的按钮,从服务获取数据并重新加载数据时。它很好用。但当我滚动时,它崩溃了。我被这件事缠住了
崩溃消息是:
2012-12-20 12:22:49.312 IAGDapp[3215:15e03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
*** First throw call stack:
(0x4b5052 0x21c7d0a 0x4a1db8 0x2b9e6 0x2b7d2 0xc80a57 0xc80b92 0xc8669e 0x7402db 0x7401af 0x489966 0x489407 0x3ec7c0 0x3ebdb4 0x3ebccb 0x1f6b879 0x1f6b93e 0xc2ba9b 0x28e0 0x2105 0x1)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
kill
你能帮我找到错误吗?问题在于你的循环
if (indexPath.row==count-1)
{
return 96;
}
数组的计数为4,您正试图在tableview中加载96行。因此,您的应用程序正在崩溃
问题不是滚动UITableView。问题是在表中加载数组。在这里,当您单击单元格的
ui按钮时。
在这里,您从服务中获得另一个数据,并且在您希望在同一个表中加载此数据之后,请执行此步骤
它崩溃了,因为在这里找不到numberOfRows和其他方法。这个新更新的数组。似乎ifMore变量没有像您期望的那样工作。由于错误日志是索引4超出了界限[0..3],很明显,tableview的数据源只包含3个元素,但在numberOfRowsInSection中返回的计数是4
作为替代解决方案,如果希望“更多”按钮始终作为数据源中的最后一行,为什么不能使用单个数组呢。只需返回
numberOfRowsInSection
方法中的tableArray.count+1
(有关更多按钮)。在cellforrowatinexpath
中,检查索引是否高于tableArray.count
,然后单击“创建”按钮。同样的,在heightforrowatinex
方法中也是如此。我同意这一点上的段落。我看不到将数据提取到数组中的部分,但是如果您忽略正确地重新加载数据/从错误的数组中获取数据/设置ifMore
变量,您的应用程序将崩溃。首先你应该检查一下。如果您认为没有问题,请在numberofrowsinssection
中设置一个断点,并查看为什么在希望有4个以上单元格的位置返回4
但实际上,我不认为您需要两个不同的数组和
ifMore
变量来实现这一目的。创建一个数组,在获取数据后更新它,重新加载表并删除totalArray和所有带有ifMore
变量的if语句。简单可能有助于提高稳定性。您能发布您的碰撞日志吗?感谢iVishal的回复。我更新了崩溃消息。它显示的像索引,但我的数组包含7个首次成功加载的对象。此错误意味着,您试图从找不到对象的位置从NSArray获取对象。换句话说:如果索引为0,则数组为空。很可能是您的代码,因为main()调用了您的代码。我猜您在从服务器调用更多数据后没有正确更新tableArray。我可以一次性显示所有词汇表,但当我滚动时会崩溃