快速将数据传递到另一个视图控制器时,值为nil

时间:2019-02-19 23:36:53

标签: swift uitableview

这是我的代码,应该将用户名数据传递给另一个视图控制器,但是当我打印值时,func prepareForSegue函数中的值是nil,有人可以告诉我我在哪里出错了,我认为这只是缺少一点东西,在此先感谢您的回答。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Get Cell Label
    let indexPath = tableview.indexPathForSelectedRow!
    let currentCell = tableview.cellForRow(at: indexPath)! as! UserTableViewCell

    valueToPass = currentCell.nameLabel.text
    print(valueToPass)
    performSegue(withIdentifier: "Profile", sender: self)



}
// Pass data part...
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if (segue.identifier == "Profile") {
    let viewController = segue.destination as! UserProfileViewController
        viewController.username = valueToPass
        print(valueToPass)
    }
}

3 个答案:

答案 0 :(得分:0)

您的UserTableViewCell大概是从您的数据源配置的-可能是用户名数组或包含用户名的结构?如果是这样,那么您应该从数据源中获取用户名,而不要从它的视图中获取(该用户名可能在进行查询时已被回收)。

例如如果您的

heap

函数分配一个值,例如

library(quanteda)
#convert readLines input to a token object
tok <- tokens(unlist(sample.lines))

#convert the token object to a frequency matrix
dfmat1 <- dfm(tok,
              remove = stopwords("english"), #remove stopwords (if wanted)
              remove_punct = TRUE, #remove punctuation (if wanted)
              tolower = T, #(change all to lowercase (if wanted)
              removeNumbers = TRUE) %>%  #remove numbers (if wanted)
   dfm_trim(min_termfreq = 4) #eliminate frequencies lower than 4 (if wanted)



# basic wordcloud
textplot_wordcloud(dfmat1)

# plot in colors with some additional options
textplot_wordcloud(dfmat1, rotation = 0.25, 
                   color = rev(RColorBrewer::brewer.pal(10, "RdBu")))

# other display options
col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))
textplot_wordcloud(dfmat1, adjust = 0.5, random_order = FALSE, 
                   color = col, rotation = FALSE)

然后尝试使用

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

因为indexPath是didSelectRowAt函数的参数。

答案 1 :(得分:0)

在情节提要中,您应该从表视图单元格到目标视图控制器进行隔离。 enter image description here 导入UIKit

ViewController1类:UIViewController,UITableViewDelegate,UITableViewDataSource {     func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {         返回5     }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    return cell
}


@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "navigate") {
        let viewController = segue.destination as! ViewController2
        viewController.name = "valueToPass"

    }
}

}

导入UIKit

ViewController2类:UIViewController {

var name : String?
override func viewDidLoad() {
    super.viewDidLoad()

    debugPrint(name)
}

答案 2 :(得分:0)

而不是直接访问单元格。只需从包含数据的yourArray中获取用户名即可。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let user = yourArrayWhichContainsUserObject[indexPath.row]
    valueToPass = user.username
    print(valueToPass)
    performSegue(withIdentifier: "Profile", sender: self)

}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if (segue.identifier == "Profile") {
    let viewController = segue.destination as! UserProfileViewController
        viewController.username = valueToPass
    }
}