1

有人可以告诉我在代码中缺少什么,因为它没有在表格视图中显示任何内容。此外,我已经在代码中设置了委托和数据源,以及 UITableView 视图出口。

。H

@interface FoodViewController :  UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *objects;
}
@property (strong, nonatomic) IBOutlet UITableView *tablewviewFood;
@property (strong, nonatomic)  NSMutableArray *objects;
@end

.m

- (void)viewDidLoad
{
[super viewDidLoad];
objects =[[NSMutableArray alloc] initWithObjects:@"Employee Services",@"Human           Resources",@"Organization",@"Policies",@"Reference",nil];
 _tablewviewFood.dataSource = self;
  _tablewviewFood.delegate = self;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_objects count];

}

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

static NSString *CellIdentifier = @"Celler";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

// Configure the cell...
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];

return cell;
4

1 回答 1

4

主要问题是viewDidLoad设置objectsnumberOfRowsInSection处理_objects.

如果没有看到您的 .h 文件以及所有属性和 ivars,我不得不猜测您声明了一个名为的属性objects以及一个名为 的 ivar objects。但是您使用的是隐式或显式@synthesize objects = _objects.

这导致有两个 ivarsobjects_objects.

声明属性,但同时删除 ivar 和@synthesize. 然后在代码中总是使用self.objects.

更新:是的,根据新发布的代码,我是正确的。

于 2013-03-02T18:48:28.457 回答