TableviewCell

时间:2016-04-26 10:16:22

标签: ios objective-c uitableview uiimage plist

我想用图片创建TableViewCell。来自我的plist的图像值。需要哪种方法?怎么样? 这是我想在tableviewcell

中添加图片的代码
    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;

}

2 个答案:

答案 0 :(得分:1)

使用此代码

NSDictionary * picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@“my”ofType:@“plist”]];

NSArray *imagesArray= [picturesDictionary allValues];
表视图中的

方法使用此

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

答案 1 :(得分:0)

您可以添加自己的单元格(New File - &gt; Cocoa类,其中xib是UITableViewCell的子类)。在那里你可以添加你想要的和你想要的地方(图像视图,更多标签)