1

I am getting a list of categories and showing them in a tableView using plist.I am also using plist to store the names of images and showing the images in cell.imageView.so far its working fine but if i am trying to add a new category i am getting a problem to add the image.I guess it is some problem with the array as the error am getting is about the beyond bounds.

- (void)viewDidLoad
{

//Reading data from plist.
self.categoryFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Categories"];
if(![[NSFileManager defaultManager] fileExistsAtPath:self.categoryFile])
{
    self.categoryFile = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"plist"];
}
self.categoryList = [[NSMutableArray alloc]initWithContentsOfFile:self.categoryFile];

//get list of images from a plist
NSString *imageNames = [[NSBundle mainBundle]pathForResource:@"ImagesNames" ofType:@"plist"];
self.imagesList = [[NSMutableArray alloc]initWithContentsOfFile:imageNames];
[toolbar release];
[self.imagesList release];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.categoryList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(!cell){
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
cell.textLabel.text = [self.categoryList objectAtIndex:indexPath.row];

//this is where am getting the error.

cell.imageView.image = [UIImage imageNamed:[self.imagesList objectAtIndex:indexPath.row]];
return cell;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
    UITextField *newCategoryName = [alertView textFieldAtIndex:0];
    NSInteger section = 0;
    NSInteger row = 0;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSString *extraContent = newCategoryName.text;
    [[self categoryList]insertObject:extraContent atIndex:row];
    [self.categoryList writeToFile:self.categoryFile atomically:YES];
    NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];
    [[self tableOfCategories]insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
    NSLog(@"%@",newCategoryName.text);
}
}
4

1 回答 1

1

根据您的问题,我猜您正在 plist 中添加类别,但您没有更新 plist 中的 ImageNames。因为对于行数,您正在使用这样的代码..

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.categoryList count];
}

在上面的代码中,因为您正在根据类别增加行。但是 imagesList 中的项目数少于 categoryList,因为您只增加了 Category。

于 2012-07-03T08:04:49.137 回答