2016-04-26 48 views
0

我想创建带有图像的TableViewCell。来自我plist的图像值。需要哪种方法?它怎么样? 这是我想在tableviewcellTableviewCell中的图像

NSArray *brend =[brends objectAtIndex:indexPath.row]; 
    NSString *logo = [[brend objectAtIndex:indexPath.row]valueForKey:@"Image"]; 

    cell.imageView.image=[UIImage imageNamed:logo]; 

我的plist中添加图片代码:

<dict> 
    <key>Mercedec</key> 
    <dict> 
     <key>Image</key> 
     <string>Mercedes.png</string> 
    </dict> 
    <key>Rena</key> 
    <dict> 
     <key>Image</key> 
     <string>Renault.png</string> 
    </dict> 

但它坠毁

由于最近开始学习Objective-C。有人可以帮我从这里出去吗?

编辑:

@interface MainTableViewController() 
@property (nonatomic, copy) NSDictionary *companylist; 
@property (nonatomic, copy) NSArray *brends; 

@end 

@implementation MainTableViewController 

@synthesize brends,companylist; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    UITableView *tableView = (id)[self.view viewWithTag:1]; 

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

    NSString *Stplist= [[NSBundle mainBundle] pathForResource:@"wunder" ofType:@"plist"]; 


    companylist = [[NSDictionary alloc]initWithContentsOfFile:Stplist]; 
    self.brends = [[self.companylist allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return brends.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    cell.textLabel.text=brends[indexPath.row]; 


    NSArray *imagesArray= [companylist allValues ]; 


    NSString *logo= [[imagesArray objectAtIndex:indexPath.row]valueForKey:@"Image"]; 

    cell.imageView.image=[UIImage imageNamed:logo]; 


    return cell; 

} 
+0

可以发布崩溃日志?什么是brends?这是一个基于你的plist的数组或字典,它应该是一本字典 – Joshua

+0

我猜Brend是'品牌' - 就像汽车品牌一样。 – headbanger

+0

您从Json获得此图片吗?您是否真的想将其存储在Plist中? – Arun

回答

1

使用这个代码

的NSDictionary * picturesDictionary = [NSDictionary的dictionaryWithContentsOfFile:[[一个NSBundle mainBundle] pathForResource:@ “我的” ofType:@ “的plist”]];

NSArray *imagesArray= [picturesDictionary allValues]; 

在表视图方法使用此

NSString *yourPicName= [[imagesArray objectAtIndex:index.row]valueForKey:@"Image"]; 
+0

它正在工作,但不是在显示的顺序 –

+0

NSDictionary是一个无序的集合。说“我添加它们的顺序”没有意义。 如果你想以特定的顺序访问这些键,那么你要么必须在你的字典旁边存储一个数组,要么得到这些键,然后对它们进行排序并使用它们按顺序访问这些值。 –

+0

例如? 只有正确放置的图像 –

0

您可以添加自己的电池(新文件 - >可可类厦门国际银行是子类的UITableViewCell)。在那里你可以添加你想要的和你想要的地方(图片查看,更多标签)

+0

我不需要单独的单元格。问题是我无法从plist中获取图像数据 –