【发布时间】: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 时,它会正确打印。只是没有出现在模拟器中
标签: ios objective-c uitableview nsmutablearray