简体   繁体   English

将数组数据从ViewController传递到TableView

[英]Pass arrays data from a ViewController to a TableView

I couldn't find this specific case where i have to pass and append two or more arrays of string into a TableView located in 2nd ViewController. 我找不到这种特殊的情况,我必须将两个或多个字符串数组传递并附加到位于第二个ViewController的TableView中。 I want to push two different strings inside two different UILabels 我想在两个不同的UILabel中推送两个不同的字符串

I have some data that gets inside through a UIAlertViewController in mainViewController 我有一些数据通过mainViewController中的UIAlertViewController进入

var name = alertController.textFields?[0].text
var number = alertController.textFields?[1].text

Then i take this data and every time i get new set of name/number i append it to arrays of string in MainVC 然后我获取这些数据,每次获取新的名称/数字集时,都会将其附加到MainVC中的字符串数组中

var playerNames = [String]()
var playerNumbers = [String]()
self.playerNames.append(name!)
self.playerNumbers.append(number!)

now i created a table view and a custom cell with .xib and i'd call it and set to custom labels like this 现在,我使用.xib创建了一个表格视图和一个自定义单元格,我将其命名并设置为这样的自定义标签

TableView in Second ViewController 第二个ViewController中的TableView

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

customCell.tableViewCellNumber.text = mainVC.playerNumbers[indexPath.row]
customCell.tableViewCellName.text = mainVC.playerNames[indexPath.row]
return customCell
}

So for every new set it makes a new row with name + number 因此,对于每个新集,它都会使用名称+数字创建一个新行 customCell图片

I tried anything from userDefaults to performForSegue but it doesnt work! 我尝试了任何从userDefaults到performForSegue的方法,但是它不起作用! I can print the arrays just fine, but it wont show me new rows at all. 我可以很好地打印数组,但是根本不会显示新行。

EDIT: actual question 编辑:实际问题

Ok I guess I misunderstood the use of NsUserDefaults... at the moment i have in viewDidLoad of my 2nd controller 好吧,我猜我误解了NsUserDefaults的使用...此刻我在第二个控制器的viewDidLoad中

if ((UserDefaults.standard.array(forKey: "playerNames")) != nil){
            let playerNames = (UserDefaults.standard.array(forKey: 
"playerNames") as? [String])!
            print(playerNames)
        }

    }

and its actually printing the new values, now i want the new appended value to show in the custom cell label.text cell.nameLabel.text = The Value how do i retrieve the value everytime and store it? 及其实际打印的新值,现在我希望新添加的值显示在自定义单元格label.text cell.nameLabel.text = The Value我如何每次都检索和存储该值? if i try with playerNames, it says it doesnt exist.. 如果我尝试使用playerNames,它说它不存在。

Did you synchronize user defaults after inputting data? 输入数据后是否同步了用户默认设置? UserDefaults.synchronize() UserDefaults.synchronize()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller 将数据从tableview传递回Viewcontroller - Pass data back to Viewcontroller from tableview 我如何将数据从SlideTableTableViewController传递到ViewController中嵌入的TableView - How i pass Data from SlideOut TableViewController to TableView embedded in ViewController 将数据从Tableview传递回ViewController文本字段Objective-C - Pass data back from tableview to viewcontroller textfield Objective - C 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 将数据从Tableview获取到包含Tableview的Viewcontroller - Get Data from Tableview to Viewcontroller containing Tableview 在Swift中的TableView和另一个ViewController之间传递数据 - Pass data between TableView and another ViewController in Swift SWIFT:对TableView进行排序并将数据传递给第二个viewController - SWIFT: sorting TableView and pass data to second viewController TableView嵌套在CollectionViewCell中的VC,我如何将数据从TableView传递回ViewController - VC with TableView nested in CollectionViewCell, how can i pass data from TableView back to ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM