im trying to make an App where the User can add a Cell and where he can add different Contents to the Cell like Name, Birthday and Hobbys for example. So far so good. But how can i show him the individual Contents of each Cell? Do I have to add a ViewController with different labels, where i load the text saved for the specific cell, for example with NSuserdefaults/Coredata? Or am I totally wrong?
What i have right now : in my viewController where i can add an item
@IBAction func doneButtonPressed(sender: AnyObject) {
name = txtFieldName.text!
vc.items.append(name)
vc.tableView.reloadData()
self.dismissViewControllerAnimated(false, completion:nil)
}
in my tableviewController:
var items = [String]();
override func viewDidLoad() {
super.viewDidLoad()
print(items)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("itemCell", forIndexPath: indexPath)
cell.textLabel!.text = items[indexPath.row]
return cell
}
You need to implement kinda this thing
NavigationController ->(segue) TableView or TVController ->(show item segue) TableView or TVController
Using this example we should have files for each tableview and cell first pair(newsvc.swift newscell.swift) second pair(newsitemvc.swift newsitemcell.swift)
So as we understand it's a MVC
We need implement some static data Create Model file (add struct and initialize it)
struct News {
var time: String?
var title: String?
var text: String?
init(time: String?, title: String?,text: String?) {
self.time = time
self.title = title
self.text = text
}
}
in second file we create data for struct
let newsData = [ News(time: "10:00", title: "Test", text: "Some text"),
News(time: "11:00", title: "Test1", text: "Some text1")]
NewsCell.swift
class NewsCell: UITableViewCell {
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var textLabel: UILabel!
var news: News! {
didSet {
timeLabel = news.time
titleLabel = news.title
textLabel.text = news.text
}
}
}
in NewsViewController we need add "News" array and make it get data from "newsData"
var newsItems: [News] = newsData
update functions
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! NewsCell
let news = newsItems[indexPath.row] as News
cell.news = news
return cell
}
after it we should add segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if segue.identifier == "showItemSegue" {
if let destViewController = segue.destinationViewController as? NewsItemViewController {
let seguedArray = newsItems[indexPath.row] as News
destViewController.newSeguedArray = seguedArray
}
}
}


If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With