如何在UIView中嵌入UITableView?

时间:2017-01-21 16:09:39

标签: ios swift uitableview uiview embed

我在使用UIView构建的自定义视图中有一个xib。我需要在上述视图中显示UITableView。起初我考虑过放置container并在其中嵌入UITableViewController。事实证明我无法将containers放在xib文件中,或者至少没有办法从IB执行此操作,因为它不会显示在视图部分中在右下方。

我可以通过编程方式创建UITableView并将其添加为视图的子视图。它按预期显示,但我似乎无法在其中添加单元格。我还尝试创建一个与UITableViewController视图关联的行为良好的storyboard,并按如下方式实例化该控制器:

let storyboard = (UIStoryboard(name: "Main", bundle: nil)) let vc = storyboard.instantiateViewControllerWithIdentifier("tableViewController") as! TestTableViewController

然后尝试访问UITableView的{​​{1}}出口。然后我在某个地方读到了nil,因为顾名思义,它会加载视图而我的vc.loadView()不会是IBOutlet。这很有效。出口时间较长nil。但是,当我将容器视图中的表添加为子视图时,它仍然不显示单元格。只有分隔线但没有内容。我已经没想完了!

修改

我没有任何nil实现,因为表是静态的。

2 个答案:

答案 0 :(得分:0)

好方法是在自定义视图中使用UITableview:

如果以编程方式添加tableview,则使用Nib或UITableview子类注册您的单元格,如

tableView.registerNib(UINib(nibName: "UITableViewCellSubclass", bundle: nil), forCellReuseIdentifier: "UITableViewCellSubclass") 

如果您使用Xib创建UITableviewCell。

tableView.registerClass(UITableViewCellSubclass.self, forCellReuseIdentifier: "UITableViewCellSubclass") // using code.
tableView.delegate = self
tableView.dataSource = self

然后使用2个必需的代表。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2 
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("UITableViewCellSubclass", forIndexPath: indexPath) as! UITableViewCellSubclass
}

希望我回答你的问题。

答案 1 :(得分:0)

目标c 您需要在viewcontroller中使用委托,如果您有viewcontroller,请将表委托:

的UIViewController 的UITableViewDelegate UITableViewDataSource

你可以使用你的功能

.list-cell {
   -fx-text-fill: red;
   -fx-strikethrough: true;
}
斯威夫特:

委托: 的UIViewController UITableViewDataSource 的UITableViewDelegate

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}


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

    return [catagorry count];    //count number of row from counting array hear cataGorry is An Array
}



- (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }

        // Here we use the provided setImageWithURL: method to load the web image
        // Ensure you use a placeholder image otherwise cells will be initialized with no image
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.textLabel.text = @"My Text";
        return cell;
    }

夫特

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

查看更多More inf