objective-c – 如何在ViewController中使用TableView?
发布时间:2020-12-14 20:05:11 所属栏目:百科 来源:网络整理
导读:在故事板我已经添加了一个表视图到我的视图控制器,我有ctrl将TableView拖到ViewController并连接“委托”和“数据源”.在(.h)文件中,我添加了 UITableViewDataSource,UITableViewDelegate但是当我运行应用程序时,我只会收到一个SIGABRT错误(?)并且应用程序
在故事板我已经添加了一个表视图到我的视图控制器,我有ctrl将TableView拖到ViewController并连接“委托”和“数据源”.在(.h)文件中,我添加了< UITableViewDataSource,UITableViewDelegate>但是当我运行应用程序时,我只会收到一个SIGABRT错误(?)并且应用程序崩溃.我该怎么办?
解决方法
到目前为止,您只需在实现文件中实现UITableViewDataSource和UITableViewDelegate即可.
所需功能如下: - (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; } Here’s the link for the Apple documentation (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |