Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
Ios 在呈现tableview swift 2之前添加一行_Ios_Swift_Uitableview_Swift2 - Fatal编程技术网

Ios 在呈现tableview swift 2之前添加一行

Ios 在呈现tableview swift 2之前添加一行,ios,swift,uitableview,swift2,Ios,Swift,Uitableview,Swift2,我试图在调用tableview(tableview:UITableView,numberofrowsinssection:Int)->Int方法之前,将一行添加到我的tableview中。我试图在viewDidLoad()方法中执行此操作,但未成功。是否可能?怎么做 这是我的密码: import UIKit class CustomTestViewController : UITableViewController { @IBOutlet var formDetailTableVie

我试图在调用
tableview(tableview:UITableView,numberofrowsinssection:Int)->Int
方法之前,将一行添加到我的tableview中。我试图在
viewDidLoad()
方法中执行此操作,但未成功。是否可能?怎么做

这是我的密码:

import UIKit

class CustomTestViewController : UITableViewController {

    @IBOutlet var formDetailTableView: UITableView!
    var data: Data?
    var firstData: FirstData?

    struct CurrentFormTableView {
        struct CellIdentifiers {
            static let MyCell = "MyCell"
            static let MyFirstCell = "MyFirstCell"
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //HERE ADD THE FIRST ROW ?
                    let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyFirstCell, forIndexPath: indexPath) as! MyFirstCell
                    cell.displayCell(firstData)
                    return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell
                    cell.displayCell(data[indexPath.row])
                    return cell
    }

}

如果我正确理解你的问题,这个问题很容易解决:)

无论数据数组中是否有数据,您总是希望在tableView中显示第一个单元格:),而您的问题是无法将第一个对象添加到数据数组中,这没关系,伙计:)

这里有一个解决方案:)

不在ViewDidLoad中执行任何操作:)只需将第一行数据对象保存在局部变量中,例如:yourCustomObject

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count + 1
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : myTestCell = tableView.dequeueReusableCellWithIdentifier("testCell")! as! myTestCell

        if indexPath.row == 0 {
             cell.nameLabel.text = yourCustomObject.property
        }
        else {
            cell.nameLabel.text = data[indexPath.row -1].property
        }
        return cell
    }
问题已解决:)快乐编码:)

工作原理: 简单,

假设您的数据数组为空:)那么它将返回计数为0:)但您总是希望显示您的第一个单元格不是:)因此将+1添加到数据数组计数:)
返回数据。计数+1

现在在
cellforrowatinexpath
中小心处理它。您不希望最终访问第一个对象的数据数组中的数据,所以请检查indexpath 0

而且您不希望最终访问数据索引之外的对象,所以请使用
data[indexath.row-1]


希望我把我的观点说清楚:)快乐编码

如果我正确理解了你的问题,这个问题很容易解决:)

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count + 1 //Add plus 1 for firstData
 }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell

   if indexPath.row == 0 {//First index will be firstData
        cell.displayCell(firstData)    
   } else { //All other cell's will be filled with data array
        cell.displayCell(data[indexPath.row - 1]) //Make sure you offset indexPath.row by 1 so you start at index 0 of data array
   }
    return cell
}
无论数据数组中是否有数据,您总是希望在tableView中显示第一个单元格:),而您的问题是无法将第一个对象添加到数据数组中,这没关系,伙计:)

这里有一个解决方案:)

不在ViewDidLoad中执行任何操作:)只需将第一行数据对象保存在局部变量中,例如:yourCustomObject

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count + 1
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : myTestCell = tableView.dequeueReusableCellWithIdentifier("testCell")! as! myTestCell

        if indexPath.row == 0 {
             cell.nameLabel.text = yourCustomObject.property
        }
        else {
            cell.nameLabel.text = data[indexPath.row -1].property
        }
        return cell
    }
问题已解决:)快乐编码:)

工作原理: 简单,

假设您的数据数组为空:)那么它将返回计数为0:)但您总是希望显示您的第一个单元格不是:)因此将+1添加到数据数组计数:)
返回数据。计数+1

现在在
cellforrowatinexpath
中小心处理它。您不希望最终访问第一个对象的数据数组中的数据,所以请检查indexpath 0

而且您不希望最终访问数据索引之外的对象,所以请使用
data[indexath.row-1]


希望我能把我的观点说清楚:)快乐编码

tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
。请注意,在将tableview的数据源设置为viewcontroller之前,不会触发此方法。请勿添加单元格。将一项添加到
数据中
并重新加载表视图。谢谢,但是我的firstData和我的数据不是相同的体系结构,我无法将一项添加到另一项中。谢谢,@Roani有关此信息,您可以做的是在
numberOfRowsInSection
中使用if-else语句,并在确定新行已准备好添加后调用
self.tableView.reloadData
。第一行不会被另一行覆盖@MikeG
tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
。请注意,在将tableview的数据源设置为viewcontroller之前,不会触发此方法。请勿添加单元格。将一项添加到
数据中
并重新加载表视图。谢谢,但是我的firstData和我的数据不是相同的体系结构,我无法将一项添加到另一项中。谢谢,@Roani有关此信息,您可以做的是在
numberOfRowsInSection
中使用if-else语句,并在确定新行已准备好添加后调用
self.tableView.reloadData
。第一行不会被另一行覆盖@米克戈!好主意!我试着告诉你you@fandro:请查看我的更新答案:)indexpath代码处的行在单元格中出错:)您不能说数据[indexpath.row]您将导致数组超出绑定异常:)请尝试数据[indexpath.row-1]好的,谢谢,我调试我的项目是因为我犯了一个错误,我会告诉你我不会忘记:)@fandro:很高兴我能帮助你,伙计:)哦!好主意!我试着告诉你you@fandro:请查看我的更新答案:)indexpath代码处的行在单元格中出错:)您不能说数据[indexpath.row]您将导致数组超出绑定异常:)请尝试数据[indexpath.row-1]好的,谢谢,我正在调试我的项目,因为我犯了一个错误,我会告诉你我不会忘记:)@fandro:很高兴我能帮助你,伙计:)谢谢你的回答:)谢谢你的回答:)
 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count + 1 //Add plus 1 for firstData
 }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell

   if indexPath.row == 0 {//First index will be firstData
        cell.displayCell(firstData)    
   } else { //All other cell's will be filled with data array
        cell.displayCell(data[indexPath.row - 1]) //Make sure you offset indexPath.row by 1 so you start at index 0 of data array
   }
    return cell
}