我知道我的问题听起来很愚蠢,但它对我的代码不起作用:
#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
答案 0 :(得分:1)
在您的代码中,未定义i
。我猜您的意思是indexPath.row
或indexPath.section
。如果数组中没有值,则问题可能是您在开始添加对象时未初始化它,然后调用类似
tableData = [[NSMutableArray alloc] initWithCapacity:20]
但这纯粹是基于所提供细节的推测。
就tableView滚动问题而言,如果您的数组包含20个对象,则索引[0 ... 19]是对象。如果numberOfRowsInSection:
中的表格视图返回tableData.count + 1
之类的内容,则最后一行的索引中不会有数据。
答案 1 :(得分:1)
你不是在任何地方创建数组,你必须先使用userids = [[NSMutableArray alloc] init]才能使用它。