通过单击按钮swift ios

时间:2018-03-08 07:01:01

标签: ios swift uitableview

我正在尝试在tableview单元格中添加一个项目,即将一个项目附加到tableview单元格的最后一个。我在stackoverflow上看了不同的答案,但没有人帮助我,我分析了 THIS ANSWER 。 我有四个数组和四个单元格,它们在表格视图中组合成一个分段控件

此代码给出了错误并终止:

  

指数超出范围

在第secCell.textLabel?.text = secArray[indexPath.row]

我想要的是:

我想添加更多单元格,并在每个单元格内部存储每个项目

import UIKit

@available(iOS 11.0, *)
class MainTableFormViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var firstArray = [String]()
var secArray = [String]()
var thirdArray = [String]()
var fourthArray = [String]()

var selectedSegment = 1

@IBAction func insertButton(_ sender: Any) {

     let item = "A new item"
    firstArray.append(item)

    firstTableView.beginUpdates()

    let indexPath:IndexPath = IndexPath(row:(self.firstArray.count - 1), section:0)

    firstTableView.insertRows(at: [indexPath], with: .left)

    TableView.endUpdates()


    firstTableView.insertRows(at: [IndexPath(row: firstArray.count - 1, section: 0)], with: .automatic) //row param is 0 for first row
    firstTableView.endUpdates()

    firstTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)


    DispatchQueue.main.async {
        self.firstTableView.reloadData()
        print("row added")
   }

    print(firstArray)
}

@IBOutlet weak var cancelMainTabForm: UIBarButtonItem!

@IBAction func cancelMainTabFormAct(_ sender: Any) {
    _ = navigationController?.popViewController(animated: true)
}

@IBOutlet weak var createMainTabForm: UIBarButtonItem!

@IBAction func createMainTabForm(_ sender: Any) {
}

@IBAction func mainSegment(_ sender: UISegmentedControl) {

    if sender.selectedSegmentIndex == 0 {
        selectedSegment = 1
    }
    if sender.selectedSegmentIndex == 1 {
        selectedSegment = 2
    }
    if sender.selectedSegmentIndex == 2 {
        selectedSegment = 3
    }
    if sender.selectedSegmentIndex == 3 {
        selectedSegment = 4
    }
    firstTableView.reloadData()

}

@IBOutlet weak var firstTableView: UITableView!


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if selectedSegment == 1 {
    return firstArray.count
    }
    if selectedSegment == 2 {
        return secArray.count
    }
    if selectedSegment == 3 {
        return thirdArray.count
    }
    else {
        return fourthArray.count
    }
}

@IBOutlet weak var tempTitle: UIButton!



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let firstCell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
    let secCell = firstTableView.dequeueReusableCell(withIdentifier: "secCell")! as UITableViewCell
    let thirdCell = firstTableView.dequeueReusableCell(withIdentifier: "thirdCell")! as UITableViewCell
    let fourthCell = firstTableView.dequeueReusableCell(withIdentifier: "fourthCell")! as UITableViewCell
    firstCell.textLabel?.text = firstArray[indexPath.row]
    secCell.textLabel?.text = secArray[indexPath.row]
    thirdCell.textLabel?.text = thirdArray[indexPath.row]
    fourthCell.textLabel?.text = fourthArray[indexPath.row]

    if selectedSegment == 1 {

        return firstCell
    }
    if selectedSegment == 2 {

        return secCell
    }
    if selectedSegment == 3 {

        return thirdCell
    }
    else  {

        return fourthCell
    }

}




func moveToViewVC() {
    let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
    let desController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewViewController") as! ViewViewController
    let newFrontViewController = UINavigationController.init(rootViewController:desController)
    revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}


override func viewDidLoad() {
    super.viewDidLoad()

    firstArray = ["a", "b", "c", "d", "e"]
    secArray = ["1", "2", "3", "4", "5"]
    thirdArray = ["ww", "ww", "c", "d", "e"]
    fourthArray = ["kk", "kk", "3", "4", "5"]

    firstTableView.delegate = self
    firstTableView.dataSource = self

    navigationController?.navigationBar.barTintColor = UIColor(red:0.00, green:0.52, blue:1.00, alpha:1.0)
    navigationController?.navigationBar.tintColor = UIColor.white

    let vcR : ViewViewController = ViewViewController()
    let temptitleText = vcR.getFormId
    print(temptitleText)

    if getTempTitleTextName?.isEmpty == false {
    tempTitle.setTitle(getTempTitleTextName, for: .normal)
} else {
    print("this is temp tiltele \(temptitleText)")
}

}

var getTempTitleTextName : String?

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

}

enter image description here

4 个答案:

答案 0 :(得分:1)

你应该这样做,

@IBAction func insertButton(_ sender: Any) {
    let item = "A new item"

    firstTableView.beginUpdates()
    firstArray.append(item)
    let indexPath:IndexPath = IndexPath(row:(self.firstArray.count - 1), section:0)
    firstTableView.insertRows(at: [indexPath], with: .left)
    firstTableView.endUpdates()

    print(firstArray)
}

尝试更新此方法,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let firstCell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
    let secCell = firstTableView.dequeueReusableCell(withIdentifier: "secCell")! as UITableViewCell
    let thirdCell = firstTableView.dequeueReusableCell(withIdentifier: "thirdCell")! as UITableViewCell
    let fourthCell = firstTableView.dequeueReusableCell(withIdentifier: "fourthCell")! as UITableViewCell

    if selectedSegment == 1 {
        firstCell.textLabel?.text = firstArray[indexPath.row]
        return firstCell
    } else if selectedSegment == 2 {
        secCell.textLabel?.text = secArray[indexPath.row]
        return secCell
    } else if selectedSegment == 3 {
        thirdCell.textLabel?.text = thirdArray[indexPath.row]
        return thirdCell
    } else  {
        fourthCell.textLabel?.text = fourthArray[indexPath.row]
        return fourthCell
    }
}

<强>更新

你需要这样做,

@IBAction func insertButton(_ sender: Any) {
    let item = "A new item"
    firstArray.append(item)
    firstTableView.reloadData()
}

这对我有用,如果有任何疑问,请告诉我。

答案 1 :(得分:0)

因为你已经调用了firstTableView.reloadData()

你可以试试这个

    import UIKit

@available(iOS 11.0, *)
class MainTableFormViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var firstArray = [String]()
var secArray = [String]()
var thirdArray = [String]()
var fourthArray = [String]()

var selectedSegment = 1

@IBAction func insertButton(_ sender: Any) {

     let item = "A new item"
    firstArray.append(item)

    firstTableView.beginUpdates()

    let indexPath:IndexPath = IndexPath(row:(self.firstArray.count - 1), section:0)

    firstTableView.insertRows(at: [indexPath], with: .left)

    TableView.endUpdates()


    firstTableView.insertRows(at: [IndexPath(row: firstArray.count - 1, section: 0)], with: .automatic) //row param is 0 for first row
    firstTableView.endUpdates()

    firstTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)


    DispatchQueue.main.async {
        self.firstTableView.reloadData()
        print("row added")
   }

    print(firstArray)
}

@IBOutlet weak var cancelMainTabForm: UIBarButtonItem!

@IBAction func cancelMainTabFormAct(_ sender: Any) {
    _ = navigationController?.popViewController(animated: true)
}

@IBOutlet weak var createMainTabForm: UIBarButtonItem!

@IBAction func createMainTabForm(_ sender: Any) {
}

@IBAction func mainSegment(_ sender: UISegmentedControl) {

    if sender.selectedSegmentIndex == 0 {
        selectedSegment = 1
    }
    if sender.selectedSegmentIndex == 1 {
        selectedSegment = 2
    }
    if sender.selectedSegmentIndex == 2 {
        selectedSegment = 3
    }
    if sender.selectedSegmentIndex == 3 {
        selectedSegment = 4
    }
    firstTableView.reloadData()

}

@IBOutlet weak var firstTableView: UITableView!


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if selectedSegment == 1 {
    return firstArray.count
    }
    if selectedSegment == 2 {
        return secArray.count
    }
    if selectedSegment == 3 {
        return thirdArray.count
    }
    else {
        return fourthArray.count
    }
}

@IBOutlet weak var tempTitle: UIButton!



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell: UITableViewCell;

    if selectedSegment == 1 {
        cell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
        firstCell.textLabel?.text = firstArray[indexPath.row]

    }
    if selectedSegment == 2 {
        cell = firstTableView.dequeueReusableCell(withIdentifier: "secCell")! as UITableViewCell
        secCell.textLabel?.text = secArray[indexPath.row]

    }
    if selectedSegment == 3 {
        cell = firstTableView.dequeueReusableCell(withIdentifier: "thirdCell")! as UITableViewCell
        thirdCell.textLabel?.text = thirdArray[indexPath.row]

    }
    else  {
        cell = firstTableView.dequeueReusableCell(withIdentifier: "fourthCell")! as UITableViewCell
        fourthCell.textLabel?.text = fourthArray[indexPath.row]

    }

    return cell;
}




func moveToViewVC() {
    let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
    let desController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewViewController") as! ViewViewController
    let newFrontViewController = UINavigationController.init(rootViewController:desController)
    revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}


override func viewDidLoad() {
    super.viewDidLoad()

    firstArray = ["a", "b", "c", "d", "e"]
    secArray = ["1", "2", "3", "4", "5"]
    thirdArray = ["ww", "ww", "c", "d", "e"]
    fourthArray = ["kk", "kk", "3", "4", "5"]

    firstTableView.delegate = self
    firstTableView.dataSource = self

    navigationController?.navigationBar.barTintColor = UIColor(red:0.00, green:0.52, blue:1.00, alpha:1.0)
    navigationController?.navigationBar.tintColor = UIColor.white

    let vcR : ViewViewController = ViewViewController()
    let temptitleText = vcR.getFormId
    print(temptitleText)

    if getTempTitleTextName?.isEmpty == false {
    tempTitle.setTitle(getTempTitleTextName, for: .normal)
} else {
    print("this is temp tiltele \(temptitleText)")
}

}

var getTempTitleTextName : String?

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

}

答案 2 :(得分:0)

每当在表视图中插入项目时,您需要更新数据源数组,然后在表视图中添加它。

firstArray.append(item)
let indexPath:IndexPath = IndexPath(row:self.firstArray.count - 1, section:0)
firstTableView.insertRows(at: [indexPath], with: .automatic)

答案 3 :(得分:0)

我尝试了所有,但没有回答帮助我,最后这样做了:

 @IBAction func insertButton(_ sender: Any) {   

    if selectedSegment == 1 {

        let item = "A new item"
        firstArray.append(item)
        firstTableView.reloadData()
    }
    if selectedSegment == 2 {

        let item = "A new item"
        secArray.append(item)
        firstTableView.reloadData()
    }
    if selectedSegment == 3 {

        let item = "A new item"
        thirdArray.append(item)
        firstTableView.reloadData()
    }
    if selectedSegment == 4 {

        let item = "A new item"
        fourthArray.append(item)
        firstTableView.reloadData()
    }
}

并将方法更改为:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let firstCell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
    let secCell = firstTableView.dequeueReusableCell(withIdentifier: "secCell")! as UITableViewCell
    let thirdCell = firstTableView.dequeueReusableCell(withIdentifier: "thirdCell")! as UITableViewCell
    let fourthCell = firstTableView.dequeueReusableCell(withIdentifier: "fourthCell")! as UITableViewCell


    if selectedSegment == 1 {
      firstCell.textLabel?.text = firstArray[indexPath.row]
        return firstCell
    }
    if selectedSegment == 2 {
        secCell.textLabel?.text = secArray[indexPath.row]
        return secCell
    }
    if selectedSegment == 3 {
    thirdCell.textLabel?.text = thirdArray[indexPath.row]
        return thirdCell
    }
    else  {
      fourthCell.textLabel?.text = fourthArray[indexPath.row]
        return fourthCell
    }

}