Ios 在UITableView中显示NSMutableArray

Ios 在UITableView中显示NSMutableArray,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在我的ViewController中创建了UITableView。在此表视图中,我想从NSMutableArray添加数据,但它似乎没有添加数据。有什么想法吗 这是我正在使用的代码 - (void)viewDidLoad { scoreArray = [[NSMutableArray alloc] init]; NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Escape", @

我在我的
ViewController
中创建了
UITableView
。在此表视图中,我想从
NSMutableArray
添加数据,但它似乎没有添加数据。有什么想法吗

这是我正在使用的代码

- (void)viewDidLoad
{
    scoreArray = [[NSMutableArray alloc] init];

    NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Escape", @"name", @"SUV", @"type", nil];
    [scoreArray addObject:myVehicle];

    [super viewDidLoad];
}

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

   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [[scoreArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.detailTextLabel.text = [[scoreArray objectAtIndex:indexPath.row] objectForKey:@"type"];

    return cell;
}

根据您的代码,您应该能够看到一行。但在这里,由于没有细节,很难确定问题

无论如何,请验证以下内容:

  • 是否设置了数据源(和委托)?这是通过XIB还是通过代码完成的
  • 您是否正确实现了
    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
样品

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [scoreArray count];
}
如果需要的节数大于1,则应实现方法
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
。默认情况下,返回一个

此外,我将稍微修改一下
viewDidLoad
方法,如下所示

- (void)viewDidLoad
{
    [super viewDidLoad]; // call it before any further initialization

    scoreArray = [[NSMutableArray alloc] init];

    NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Escape", @"name", @"SUV", @"type", nil];
    [scoreArray addObject:myVehicle];
}

确保.h文件指示这是UITableView的委托:

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>


您是否设置了委托和数据源?1:按代码2:否尚未实现-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)。我的操作与此完全相同,但UIViewTable中没有显示任何内容。只是为了确保,viewtable应该是动态属性权限?否。请删除
@dynamic
。只需在界面中使用
@property
,记住链接正确的表。我的操作与此完全相同,但UIViewTable中没有显示任何内容。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [scoreArray count];
}