【发布时间】:2013-12-24 16:52:37
【问题描述】:
在 XCode 5 中,当您添加一个 UITableViewController 具有多个包含标签等的原型单元格时,会显示内容,因为 UITableViewController 会自动设置数据表视图的源和委托,并向其发送 reloadData 消息。
iOS Developer Library - Basics of Table View Creation
但是当您将 UITableView 拖到 UIViewController 时,我相信您必须创建类似于下面的默认方法的实例方法,以便重新加载和制作内容可见。
我的问题是,如何将 UITableView 定位在 UIViewController 中的某个位置,并设置其数据源和委托,并重新加载并正确显示其内容?
这是 UITableViewController 的默认代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
这是我的代码,它适用于 UITableViewController 但不适用于它包含的 UITableView 的 UIViewController:
...
@property (nonatomic, strong) NSArray *menuItems;
...
- (void) viewDidLoad
{
[super viewDidLoad];
self.menuItems = @[@"rowOne", @"rowTwo", @"rowThree", @"rowFour"];
}
...
- (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 [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
【问题讨论】:
-
您是否将代理和数据源属性绑定到您的视图控制器?
标签: ios iphone objective-c uitableview