如何在swift中将数据插入到tableview中 [英] How do I insert data into a tableview in swift

查看:301
本文介绍了如何在swift中将数据插入到tableview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用tableview控制器制作了一个tableview,但我真的需要知道如何在视图控制器中使用自定义大小的tableview做同样的事情。我关心的只是插入数据,因为我需要在其中插入部分。



我尝试了什么:



我尝试使用tableview控制器,但显然它与tableview

I have already made a tableview with tableview controller but I am really in need to know how to do the same thing with a custom sized tableview inside a view controller. my concern is only about inserting data because I need to have sections in it.

What I have tried:

I tried with tableview controller but apparently it is a bit different with tableview

推荐答案

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    var menu: Array = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

          menu = ["home", "Logout"]
    }

 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menu.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell 
        cell.labelmanu.text = menu[indexPath.row]
        return cell
    }





使用数组在tableview中声明数据,委托和数据源方法相应地更新和设置数据



Use the array to declare your data in tableview and delegates and datasource method to update and set the data accordingly


这篇关于如何在swift中将数据插入到tableview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆