我剛剛學習iPhone的發展,所以請原諒我什麼可能是一個初學者的錯誤。我周圍搜索,沒有找到任何具體的問題,所以希望這是一個簡單的修復。幫助與iPhone開發 - 超越界限錯誤
這本書的例子是通過一個數組硬編碼來建立表格。不幸的是,它從來沒有真正告訴你如何從URL中獲取數據,所以我不得不查看那個,這就是我的問題出現的地方。
當我在xcode中調試時,似乎count是正確的,所以我不明白爲什麼它出界了?
這是錯誤消息:
2010-05-03 12:50:42.705 Simple Table[3310:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'
我的URL返回以下字符串:
first,second,third,fourth
,這裏是用書的工作示例的iphone代碼註釋掉
#import "Simple_TableViewController.h"
@implementation Simple_TableViewController
@synthesize listData;
- (void)viewDidLoad
{
/*
//working code from book
NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy", @"Bashful", @"Bashful", @"Happy",
@"Doc", @"Grumpy", @"Thorin", @"Dorin", @"Norin", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin",
@"Gloin", @"Bifur", @"Bofur", @"Bombur", nil];
self.listData = array;
[array release];
*/
//code from interwebz that crashes
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://www.mysite.com/folder/iphone-test.php"];
NSURL *url = [[NSURL alloc] initWithString:urlstr];
NSString *ans = [NSString stringWithContentsOfURL:url];
NSArray *listItems = [ans componentsSeparatedByString:@","];
self.listData = listItems;
[urlstr release];
[url release];
[ans release];
[listItems release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.listData = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[listData release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
@end
我沒有看到問題。你在incellForRowAtIndexPath的return語句處停止調試器,並在那裏檢查listData(和row)嗎?確保listData非零,並且仍然有數據。 – progrmr 2010-05-04 03:03:17
listItems在這裏嘗試發佈發佈聲明,看看會發生什麼 – Daniel 2010-05-04 03:34:14
@Daniel:aha,listItems是autoreleased,不需要發佈它。你應該把它作爲答案。 – progrmr 2010-05-04 04:41:01