从 NSMutableArray 填充 UITableView

Posted

技术标签:

【中文标题】从 NSMutableArray 填充 UITableView【英文标题】:Populate UITableView from NSMutableArray 【发布时间】:2014-10-22 19:36:26 【问题描述】:

有一个问题,数组值没有显示在我的 tableview 单元格中,但可以使用 NSLog 正确打印出来。提前感谢您的帮助!

TableViewCell .h

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *label;

@end

TableViewController.h

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) NSMutableArray *imagesArray;
@property(nonatomic, strong) NSMutableArray *namesArray;

@end

TableViewController.m

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize primaryWeaponNames = _primaryWeaponNames;

- (void)viewDidLoad 
[super viewDidLoad];

[self setupArrays];



- (void)setupArrays 

    _namesArray = [[NSMutableArray alloc] initWithObjects:
                            @"NAME1", @"NAME2", @"NAME3"
                            nil];


    self.imagesArray = [[NSMutableArray alloc] initWithObjects:
                            @"IMG1", @"IMG2", @"IMG3"
                            nil];


- (void)didReceiveMemoryWarning 
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
// Return the number of sections.
return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
// Return the number of rows in the section.
return _namesArray.count;


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

static NSString *CellIdentifier = @"TableViewCell";

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


cell.nameLabel.text = [NSString stringWithFormat:@"%@", [_namesArray objectAtIndex:indexPath.row]];
NSLog(@"%@", _namesArray[indexPath.row]);

cell.image.image = [self.imagesArray objectAtIndex:indexPath.row];

[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];

return cell;

【问题讨论】:

你实现了UITableViewDataSource 方法吗? 是的,在头文件中 你做了什么来调试它?是否调用了您的 dataSource 方法? numberOfRownInSection 返回什么值? 你做过调试吗?在numberOfRowsInSection 方法中放置一个断点。它被称为? _nameArray 是非 nil 并且它是否包含任何对象? 是的,_nameArray 具有正确的值。当我打印 NSLog 时,它会正确打印。只是没有出现在模拟器中 【参考方案1】:

当你在 xib 文件中创建单元格时,你应该在 viewDidLoad 中注册 nib。由于您没有这样做,因此 dequeue 方法将返回 nil,并且您只是创建了一个默认单元格(在 if (cell == nil) 子句中)而不是您的自定义单元格。默认单元格没有 nameLabel 或 image 属性,因此设置这些网点值的行不会做任何事情。

[self.tableView registerNib:[UINib nibWithNibName:@"Your nib name here" bundle:nil] forCellReuseIdentifier:@"TableViewCell];

【讨论】:

好的,错误是 - '[ setValue:forUndefinedKey:]: 这个类不符合关键图像的关键值编码。' 我正在取出所有图像内容,所以我只使用标签【参考方案2】:

在您的TableViewController 类中,您需要包含

self.tableView.delegate = self;
self.tableView.datasource = self;

在您的 viewDidLoad 方法中。或者您可以在界面生成器中执行此操作,方法是右键单击将表格视图控制器中的表格视图拖动到文件的所有者并设置委托,然后重复数据源

【讨论】:

添加了这些,仍然没有 不,你没有。 OP 使用 UITableViewController,默认设置为数据源和委托。 这只是一个想法。 @Seslyn,当您在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中设置断点时,它会被调用吗?单步执行,它是返回一个单元格还是 nil?

以上是关于从 NSMutableArray 填充 UITableView的主要内容,如果未能解决你的问题,请参考以下文章

自动发布的 NSMutableArray 未填充

UITableView + NSMutableArray 奇怪的行为

为啥这个 NSMutableArray 没有被填充?

使用 NSMutableArray 中的数据填充 UITableView

从 UITableView NSMutableArray 和 ManagedObjectContext 中删除行

用指针填充的 NSMutableArray