【问题标题】:Image in TableviewCellTableviewCell 中的图像
【发布时间】: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


【解决方案1】:

您可以添加自己的单元格(新文件 --> 带有 xib 的 Cocoa 类,它是 UITableViewCell 的子类)。在那里你可以添加你想要的和你想要的(图像视图,更多标签)

【讨论】:

  • 我不需要单独的单元格。问题是我无法从 plist 中获取图像数据
【解决方案2】:

使用此代码

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

NSArray *imagesArray= [picturesDictionary allValues];

在表格视图方法中使用 this

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

【讨论】:

  • NSDictionary 是一个无序集合。说“我添加它们的顺序”没有任何意义。如果您想按特定顺序访问键,那么您要么必须在字典旁边存储一个数组,要么获取键然后对它们进行排序并使用它来按该顺序访问值。
  • Root ---- Dictionary PhotosArray ----- Array Item 0 ------ String -- image.jpg
  • NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]]; NSArray *imageArray = [picturesDictionary objectForKey:@"PhotosArray"]; PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
猜你喜欢
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多