【问题标题】:Create storyboard screen with two inherit classes使用两个继承类创建故事板屏幕
【发布时间】:2016-01-15 17:14:00
【问题描述】:
我是 iOS 编程新手,在一个屏幕上连接两个单独的屏幕模型时遇到问题。我有一个表格视图和典型的视图控制器。当我想要这个屏幕的控制元素时,我需要两个单独的类,一个从 UITableView 继承,第二个从 UIViewController 继承,因为在 Objective-C 中我无法创建从其他两个类继承的类。如何从视图控制器类控制表视图类。我必须说,简单的组合并不能解决我的问题,因为我不知道如何将类与屏幕模型动态连接。
感谢您的帮助。
【问题讨论】:
标签:
ios
objective-c
inheritance
tableview
viewcontroller
【解决方案1】:
你必须使用 UITableViewDelegate 和 UITableViewDataSource
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
并为您的 UITableView .delegate 和 .datasource 设置或将其链接到故事板
【解决方案2】:
感谢帮助,但这个答案必须完成,因为在视图控制器的代码中必须添加一些功能:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [regions count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
Region *region = [regions objectAtIndex:section];
return [region.timeZoneWrappers count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
Region *region = [regions objectAtIndex:section];
return [region name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]];
}
Region *region = [regions objectAtIndex:indexPath.section];
TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneWrapper.localeName;
return cell;
}