Hi,
I have a table view controller which shows 2 sections with 2 rows in each. It display well initially but causes exceptions when I scroll them up.
The following is my code. I checked the variable onlineBooks - it's "out of scope" when exception happened in cellForRowAtIndexPath and its type was changed from NSArray* to NSCFString*
Can anyone suggest me where could be the problem?
I have a table view controller which shows 2 sections with 2 rows in each. It display well initially but causes exceptions when I scroll them up.
The following is my code. I checked the variable onlineBooks - it's "out of scope" when exception happened in cellForRowAtIndexPath and its type was changed from NSArray* to NSCFString*
Can anyone suggest me where could be the problem?
Code:
@interface RootViewController : UITableViewController {
NSArray *onlineBooks;
NSArray *localBooks;
}
@property (nonatomic, retain) NSArray *onlineBooks;
@property (nonatomic, retain) NSArray *localBooks;
@end
...
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = NSLocalizedString(@"Book Library", @"Book Library title");
onlineBooks = [NSArray arrayWithObjects:@"Online Book 1", @"Online Book 2", nil];
localBooks = [NSArray arrayWithObjects:@"Local Book 1", @"Local Book 2", nil];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [onlineBooks count];
} else {
return [localBooks count];
}
//return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Online Books";
} else {
return @"Local Books";
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSString *book;
if (indexPath.section == 0) {
if (indexPath.row < [onlineBooks count]) { // !!!Exception here!!!
book = [onlineBooks objectAtIndex:indexPath.row];
}
} else {
if (indexPath.row < [localBooks count]) {
book = [localBooks objectAtIndex:indexPath.row];
}
}
cell.textLabel.text = book;
return cell;
}
...