Why the “Use of undeclared type UITableView” happe

2020-05-31 07:26发布

I am new to Swift. I created a second swift file call DeviceInfo.swift and add a UIViewcontroller in Main.storyboard.

I also add a UITableView in UIViewcontroller(DeviceInfo).

But the error Use of undeclared type 'UITableView' show after I connect the UITableView to DeviceInfo.swift via command + left. The error show like the following. enter image description here

Question

1. Why the Use of undeclared type 'UITableView' happened ? How to solve it ?

2. Show I connect the dataSource and delegate to UIViewcontroller(DeviceInfo)?

Thanks in advance.

3条回答
冷血范
2楼-- · 2020-05-31 07:43

You need to add import UIKit at the top of your file, in the area where you've already got import Foundation. Without UIKit imported, your object doesn't know what a UITableView is.

查看更多
迷人小祖宗
3楼-- · 2020-05-31 08:00

1.import UIKit

2.To connect delegate and dataSource, click the point and drag to your tableView. enter image description here

查看更多
We Are One
4楼-- · 2020-05-31 08:02

UITableView in in UIKit framework, you didn't imported that. SO import UIKit.

In your viewDidLod:

self.tableView.dataSource = self;
self.tableView.delegate = self;

In your .h file you can do:

@interface YourClassName : UIViewController<UITableViewDataSource,UITableViewDelegate>

And make sure you connected the tableView with an IBOutlet. #import <UIKit/UIKit.h>

查看更多
登录 后发表回答