找到你要的答案

Q:re-populating a tableview with new array elements when a cell is clicked in swift

Q:重新填充一个新数组元素TableView当细胞点击迅速

thanks for all the help so far. I need, when a cell in UITableView is clicked, to re-populate the view with an array read from another class - just can find a way to refresh the view. Code as follows:

Thanks in advance - the help so far has been great for this newbie.

import UIKit

class  SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!

let textCellIdentifier = "TextCell"

var catRet = XnYCategories.mainCats("main")

//var catRet = XnYCategories.subCats("Sport")
    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

// MARK:  UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1

}

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

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

    let row = indexPath.row
    cell.textLabel?.text = catRet[row]
    return cell

}

// MARK:  UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let row = indexPath.row
    //let currentCell = tableView.cellForRowAtIndexPath(indexPath)
    //var selectedText = currentCell!.textLabel?.text
    //println(selectedText)
    let catRet2 = XnYCategories.mainCats(catRet[row])
    println(catRet2)
    println(catRet[row])

    //catRet = catRet2

}

}

感谢所有的帮助到目前为止。我需要的,当在UITableView细胞时,重新填充视图与另一类读取数组就可以找到一种方法来刷新视图。代码如下:

谢谢您的帮助,到目前为止已经很大了这个新手。

import UIKit

class  SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var tableView: UITableView!

let textCellIdentifier = "TextCell"

var catRet = XnYCategories.mainCats("main")

//var catRet = XnYCategories.subCats("Sport")
    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

// MARK:  UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1

}

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

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

    let row = indexPath.row
    cell.textLabel?.text = catRet[row]
    return cell

}

// MARK:  UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let row = indexPath.row
    //let currentCell = tableView.cellForRowAtIndexPath(indexPath)
    //var selectedText = currentCell!.textLabel?.text
    //println(selectedText)
    let catRet2 = XnYCategories.mainCats(catRet[row])
    println(catRet2)
    println(catRet[row])

    //catRet = catRet2

}

}
answer1: 回答1:

Call reloadData() on your tableView as follow

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let row = indexPath.row
    //let currentCell = tableView.cellForRowAtIndexPath(indexPath)
    //var selectedText = currentCell!.textLabel?.text
    //println(selectedText)
    let catRet2 = XnYCategories.mainCats(catRet[row])
    println(catRet2)
    println(catRet[row])

    // *** New code added ***
    // remove the comment
    catRet = catRet2

    // call reloadData()
    tableView.reloadData()
    // *** New code added ***
}

叫上你的TableView reloaddata()如下

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let row = indexPath.row
    //let currentCell = tableView.cellForRowAtIndexPath(indexPath)
    //var selectedText = currentCell!.textLabel?.text
    //println(selectedText)
    let catRet2 = XnYCategories.mainCats(catRet[row])
    println(catRet2)
    println(catRet[row])

    // *** New code added ***
    // remove the comment
    catRet = catRet2

    // call reloadData()
    tableView.reloadData()
    // *** New code added ***
}
answer2: 回答2:

just do this:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let row = indexPath.row

        catRet = XnYCategories.mainCats(catRet[row])

        tableView.reloadData()

    }

就这样做:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let row = indexPath.row

        catRet = XnYCategories.mainCats(catRet[row])

        tableView.reloadData()

    }
ios  uitableview  swift  nsarray