Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 如何在Swift中使数据源成为字符串?_Uitableview_Swift_Ios8_Xcode6 - Fatal编程技术网

Uitableview 如何在Swift中使数据源成为字符串?

Uitableview 如何在Swift中使数据源成为字符串?,uitableview,swift,ios8,xcode6,Uitableview,Swift,Ios8,Xcode6,我使用的是UITableView,也就是UITableViewDataSource。我的数据源是 theTableView.dataSource = self 但是我想让UITableView从一个名为“phone”的变量(字符串)中获取数据。当前执行此操作时,字符串的错误与UITableViewDataSource不同 如何让UITableView从字符串变量获取数据 谢谢-希望这对其他人也有用 对于一些对其他寻求相同解决方案的人来说无关紧要的背景:var“phone”是通过

我使用的是UITableView,也就是UITableViewDataSource。我的数据源是

        theTableView.dataSource = self
但是我想让UITableView从一个名为“phone”的变量(字符串)中获取数据。当前执行此操作时,字符串的错误与UITableViewDataSource不同

如何让UITableView从字符串变量获取数据

谢谢-希望这对其他人也有用


对于一些对其他寻求相同解决方案的人来说无关紧要的背景:var“phone”是通过contacts框架从用户联系人中提取的电话号码。我正在尝试让UITableView在每次选择新电话号码时都接收新电话号码。

好的,您需要将tableView的数据源设置为符合UITableViewDataSource协议的类。然后定义tableViewtableView:UITableView,cellforrowatinedexpath indexath:NSIndexPath函数

例如:

import Foundation

class Example : UITableView, UITableViewDataSource, UITableViewDelegate
{
    var dataSource = [ "hello", "how", "are", "you" ]

    // MARK: Lifecycle
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)

    }

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.dataSource = self
        self.delegate = self
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: Data Source / Delegates
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellId")

        cell.textLabel?.text = self.dataSource[indexPath.row]

        return cell
    }
}

我猜你还不明白这个概念。UITableViewDataSource是一个类,它具有多个方法,在显示单个单元格时从表中调用这些方法。您需要实现它。所有必需的方法都已经实现,代码是无错误的-我应该澄清这一点。