xcode tableView numberOfRowsInSection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6445361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tableView numberOfRowsInSection
提问by XcodeMania
I have this error in xcode:
我在 xcode 中有这个错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:
I using this code:
我使用这个代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
int num_rows = 0;
switch (section) {
case 0:{
num_rows = [messageReceive count];
}
break;
case 1:{
num_rows = [messageSend count];
}
break;
return num_rows;
}
but i try many code but the same error. I using a UITableView to get message from a script php with Json. In "messageReceive" sometimes i have 0 or 1 or many messages. the same for messageSend.
但我尝试了很多代码但同样的错误。我使用 UITableView 从带有 Json 的脚本 php 获取消息。在“messageReceive”中,有时我有 0 条或 1 条或多条消息。messageSend 也是一样。
thx
谢谢
this is my cellForRowAtIndexPath
这是我的 cellForRowAtIndexPath
// Configure the cell.
NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
switch ( indexPath.section )
{
case 0:
if (pseudoLabel.text =[dictMessSend objectForKey:@"sender"] )
{cell.detailTextLabel.text = [dictMessSend objectForKey:@"receiver"];
cell.text= [dictMessSend objectForKey:@"message"];
}
break;
case 1:
if (pseudoLabel.text =[dictMessReceive objectForKey:@"receiver"] )
{cell.detailTextLabel.text = [dictMessReceive objectForKey:@"sender"];
cell.text= [dictMessReceive objectForKey:@"message"];
}
else {
//cell.text = @"No message";
}
break;
}
return cell;
回答by Joe
Where is the exception being raised? No where in the code you posted could that exception be raised unless you implemented a custom count method. So I will assume the exception is being raised in tableView:cellForRowAtIndexPath:
. Inside of that method (or any other similar methods) make sure you use the same logic.
异常在哪里引发?除非您实现了自定义计数方法,否则您发布的代码中的任何地方都不会引发该异常。所以我会假设异常是在tableView:cellForRowAtIndexPath:
. 在该方法(或任何其他类似方法)中,确保使用相同的逻辑。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:{
//messageReceive logic
}
break;
case 1:{
//messageSend logic
}
break;
}
}
Next thing you want to do is make sure that messageReceive and messageSend are not mutable (your console output shows it was mutable). If they are properties then they should be an NSArray and set to copy not retain (ex. @property(nonatomic, copy) NSArray *messageReceive;
otherwise the code should look like messageReceive = [sourceArray copy];
接下来您要做的是确保 messageReceive 和 messageSend 不可变(您的控制台输出显示它是可变的)。如果它们是属性,那么它们应该是一个 NSArray 并设置为复制而不保留(例如,@property(nonatomic, copy) NSArray *messageReceive;
否则代码应该看起来像messageReceive = [sourceArray copy];
Update:
更新:
The update you provide shows the problem is on the following line
您提供的更新显示问题出在以下行
NSDictionary * dictMessReceive = [self.messageReceive objectAtIndex:indexPath.row];
NSDictionary * dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
You can not access both arrays with the same index, move that into the switch statement
您不能使用相同的索引访问两个数组,请将其移动到 switch 语句中
NSDictionary * dictMessReceive = nil;
NSDictionary * dictMessSend = nil;
switch ( indexPath.section )
{
case 0:
dictMessageReceive = [self.messageReceive objectAtIndex:indexPath.row];
//...
break;
case 1:
dictMessSend = [self.messageSend objectAtIndex:indexPath.row];
//...
break;
}
回答by Daniel
post your cellForRowAtIndexPath method, thats where you are going wrong, you are probably using the wrong array for certain sections...So for section 0 you use messages received (which might be 5) and in cellForRowAtIndexPath you are reading from the messages sent array which is less that 5, and so you get the out of bounds exception..
发布您的 cellForRowAtIndexPath 方法,这就是您出错的地方,您可能在某些部分使用了错误的数组...因此,对于第 0 部分,您使用收到的消息(可能是 5),而在 cellForRowAtIndexPath 中,您正在从发送的消息数组中读取小于 5,所以你得到越界异常..