我得到一个类别列表并使用plist在tableView中显示它们。我也使用plist来存储图像的名称并在cell.imageView.so中显示图像远其工作正常但如果我正在尝试添加一个新的类别我有一个问题是添加图像。我猜这是数组的一些问题,因为我得到的错误是超出界限。
- (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];
//这是我收到错误的地方。
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);
}
}
答案 0 :(得分:1)
根据您的问题,我猜您是在plist中添加类别但是您没有更新plist中的ImageNames。因为您使用的代码行数是这样的..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.categoryList count];
}
在上面的代码中,因为您根据类别增加行。但是物品的数量 imagesList小于categoryList,因为您只增加Category。