在 UIViewController 中设置 UITableView

Posted

技术标签:

【中文标题】在 UIViewController 中设置 UITableView【英文标题】:Setting up a UITableView within a UIViewController 【发布时间】:2014-06-19 19:49:39 【问题描述】:

我有一个名为 QuickLookView 的ViewController,我需要在其中实现一个表格视图。我已经在我的头文件中声明了 tableview:

@interface QuickLookView : UIViewController <UITableViewDelegate, UITableViewDataSource>
...
@property (weak, nonatomic) IBOutlet UITableView *medTableView;
@end

QuicklookView.m我合成了medTableView并添加了标准的UITableView方法:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    NSMutableArray * timeline =  [gCore getDosageContainer];

    return [timeline count];



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

static NSString *CellIdentifier = @"medicineCell";
UITableViewCell *cell = [medTableView dequeueReusableCellWithIdentifier:CellIdentifier      forIndexPath:indexPath];

NSMutableArray * timeline =  [gCore getDosageContainer];

GuardianPatientDosageTimeline * d;

d = (GuardianPatientDosageTimeline *) [timeline objectAtIndex: indexPath.row];
NSString *MedTimeLine = [NSString stringWithFormat:@"%.02f %@ of %@",d->amount,d->units,d-  >name];

if (cell == nil)

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



cell.textLabel.font = [UIFont fontWithName:@"ProximaNova-Regular" size:15];
cell.textLabel.text = MedTimeLine;//"toDoItem.itemName;



return cell;



这就是我的委托和数据源的连接方式:

奥特莱斯

数据源------------------->快速查看视图

委托--------------------->快速查看视图

参考网点

medTableView---------------->快速查看视图

(本来想发图的,但是我的名气还不够大……)

但在尝试了很多方法后,我仍然收到错误消息。从用普通整数替换我的return [timeline count]; 到将事物与谁知道什么联系起来。 “timeline”和“gCore”中的所有数据都在这个文件之外使用,所以我知道里面有数据。

这是错误:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIView tableView:numberOfRowsInSection:]:

【问题讨论】:

你试过在“NSMutableArray * timeline = [gCore getDosageContainer];”上放置断点吗?看看实际计数是多少?它也应该是一个 NSArray,因为你没有修改它。实际上,从头开始 - 你只需要这样做:“return [[gCore getDosageContainer] count];” @Nick 我刚刚尝试了“return [[gCore getDosageContainer] count];”我得到了同样的错误信息。当我更改该部分而不是“返回 1”时,我什至收到错误消息;例如。这让我相信我的授权/数据源有问题,我只是不知道是什么。 【参考方案1】:

在表格视图的属性检查器中,它看起来像这样吗:

【讨论】:

是的,这正是我的样子,只是带有适当的本地名称 你得到的错误还有更多,例如“无法识别的选择器发送到实例”。如果是这样,我建议你看看***.com/questions/17350280/… 并尝试一些建议(异常断点/僵尸转身)【参考方案2】:

尝试从界面生成器中删除委托和数据源出口,并以编程方式设置它

-(void)viewDidLoad //.. self.tableView.delegate = self; self.tableView.dataSource = 自我;

您似乎将该插座连接到您的视图而不是视图控制器。

【讨论】:

不,这也不起作用。但我真的很感激这次尝试! 您确定从情节提要中删除了委托和数据源出口吗? 是的,我继续按下连接旁边的小“x”,然后两者都消失了。【参考方案3】:

原来我在布局的匆忙中不小心将两个UITableView 放在了彼此的顶部。这解释了为什么没有调用numberOfRowsInSection - 因为它指的是隐藏在我的主要UITableView 下的辅助UITableView。上述所有其他方法都可以将UITableView 连接到UIViewController,我的问题只是用户错误。

【讨论】:

以上是关于在 UIViewController 中设置 UITableView的主要内容,如果未能解决你的问题,请参考以下文章

在导航控制器内的 UIViewController 中设置背景

在 NSObject 类中设置协议并在 UIViewController 类中调用它们

键盘显示时在 UIViewController 中设置 Tableview y 位置

UIView 子类在 Swift 中设置自己的高度 + UIViewController 中的约束 + 情节提要

如何在没有情节提要的 Xamarin.iOS UIViewController 中设置 RestorationClass?

如何在剑道 UI 网格中设置多行列