【发布时间】:2014-01-22 16:10:18
【问题描述】:
我知道我的问题听起来很愚蠢,但它不适用于我的代码:
#import "TermineViewController.h"
#import "DetailViewController.h"
@interface TermineViewController ()
@end
@implementation TermineViewController {
NSArray *tableData;
int i;
NSString *userid;
NSString *selection;
NSMutableArray *userids;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString: @"http://example.com"];
NSString *dataString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
tableData = [dataString componentsSeparatedByString:@"--"];
NSLog(userids);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ([tableData count])/4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.contentView.backgroundColor = [UIColor blackColor];
cell.accessoryView.backgroundColor = [UIColor blackColor];
[self.tableView setSeparatorColor:[UIColor whiteColor]];
self.view.backgroundColor = [UIColor blackColor];
//Declare all Labels
UILabel *name = (UILabel *)[cell viewWithTag:100];
UILabel *service = (UILabel *)[cell viewWithTag:200];
UILabel *time = (UILabel *)[cell viewWithTag:300];
//Write into Labels and add one to int i (declared in implementation)
name.text = [tableData objectAtIndex:i];
i++;
service.text = [tableData objectAtIndex:i];
i++;
time.text = [tableData objectAtIndex:i];
i++;
userid = [tableData objectAtIndex:i];
NSString *usrid = [NSString stringWithFormat:@"%@", userid];
[userids addObject:usrid];
i++;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selection = userids[indexPath.row];
NSLog(@"User selected %@", selection);
NSLog(userids[0]);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"details"]) {
DetailViewController *destViewController = (DetailViewController *)[segue destinationViewController];
destViewController.userid = selection;
}
}
@end
所以,这里有两个问题:第一个是我的 NSMutableArray 中没有任何值(顺便说一句,它是在 @implementation 中声明的)。第二个是如果用户在 TableView 中滚动,我会收到以下错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因: '* -[__NSArrayM objectAtIndex:]:索引 20 超出范围 [0 .. 19]' * 首先抛出调用堆栈:[...]
不知道怎么回事?是的,我的数组中有 20 个值,但第 20 个存在!
非常感谢!!!!
你好,Kitzng
【问题讨论】:
-
Objective-C 中的数组是从 0 开始索引的,这意味着第一个元素的索引为 0,第 20 个元素的索引为 19。
-
检查
- tableView:numberOfRowsInSection:是否返回[tableData count]。 -
在此处粘贴您的函数 - tableView:numberOfRowsInSection: 代码。这样我们就可以检查了。
-
userid = [tableData objectAtIndex:i]:什么是“userid”、“tableData”和“i”?
-
我的 NSMutableArray 中没有任何值 -- 什么 NSMutableArray??
标签: ios objective-c uitableview nsmutablearray