2016-04-26 77 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中獲取圖像數據 –