gpt4 book ai didi

xcode - 加载数据到 UITableView

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:10 24 4
gpt4 key购买 nike

我的数据是这样保存的:

var mineSpillere = ["erik", "tom", "phil"]

我怎样才能通过按这样的 UIButton 将数据添加到 UITableView?:

@IBAction func backButtonAction(sender: AnyObject) {
// Press this button here to add the "mineSpillere" data to myTableView
}

最佳答案

在IBAction中设置 TableView 的数据源。像这样:

@IBAction func backButtonAction(sender: AnyObject) {
myTableView.dataSource = self
// Loading from NSUserDefaults:
// Please name it something better than array I couldn't come up with any names.
if let array = NSUserDefaults.standardUserDefaults().arrayForKey("key") as? [String]
{
// The key exists in user defaults
}
else
{
// The key doesn't exist in the user defaults, do some error handling here.
}
}

然后实现数据源:

extension MyVC : UITableViewDataSource
{
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return mineSpillere.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
cell.textLabel!.text = mineSpillere[indexPath.row]
return cell
}
}

关于xcode - 加载数据到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com