【发布时间】:2016-08-20 02:34:10
【问题描述】:
我想用图像创建TableViewCell。我的 plist 中的图像值。需要什么方法?怎么做?
这是我想在tableviewcell 中添加图像的代码
NSArray *brend =[brends objectAtIndex:indexPath.row];
NSString *logo = [[brend objectAtIndex:indexPath.row]valueForKey:@"Image"];
cell.imageView.image=[UIImage imageNamed:logo];
我的列表:
<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;
}
【问题讨论】:
-
可以发布崩溃日志吗?什么是brends?它是基于您的 plist 的数组还是字典,它应该是字典
-
我猜 brend 是“品牌”——就像汽车的品牌一样。
-
你从 Json 得到这张图片?你真的要让它存储在 Plist 中吗?
-
它在字典中,因此您可以在字典中获取这些图像,然后使用对象作为键。请在单元格中发布您的整个代码,以便 rowatindexpath 完成
-
我添加了 TableviewController.m。在我的问题中请检查@Arun
标签: ios objective-c uitableview uiimage plist