[英]UITableView doesn't appear correctly - why?
我使用Xcode 5和情节提要,在其中使用导航控制器和表格视图,并在以下名为RootViewController.h
代码中,
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController
@property NSMutableArray *objects;
@end
和RootViewController.m
,
#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d", _objects.count);
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"A";
return cell;
}
@end
Xcode说“ Build Succeeded”,但是结果屏幕上的行没有正确显示。
它返回空行和名为Root View Controller
标题。
为什么?
我想我在numberOfRowsInSection
, cellForRowAtIndexPath
和numberOfSectionInTableView
正确编写了三个必需的方法,并且我认为不需要在头文件中定义协议,因为它已经在父UITableViewController
定义了。
并且由于某些原因,未执行numberOfRowsInSection
方法中的调试NSLog函数。
那我在这里想念什么? 我该如何解决?
谢谢。
问题是_objects
为_objects
当您尝试将对象添加为_objects
时,结果为_objects
。
因此,您需要创建数组而不是添加数组。 更改此行
[_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];
对此
_objects = [NSMutableArray arrayWithObjects:@"Apple", @"Google", @"Intel", nil];
在情节提要中,您还需要设置UITableViewCell的单元格标识符(有关如何执行此操作的大量教程)。
放入viewDidLoad
self.title = @"some title";
更改NavigationBar的标题
您需要先创建对象,然后才能向其中添加任何内容:
- (void)viewDidLoad
{
[super viewDidLoad];
_objects = [NSMutableArray new];
[_objects addObjectsFromArray:@[@"Apple",@"Google",@"Intel"]];
}
您是否设置了tableview委托? 您是否在情节提要中正确设置了tableviewcell?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.