I have eight, different static cells in a tableview. I am trying to perform different actions with six of the cells. Two of them open an alert view, two open safari, one clears a cache, and the last one opens a share popover. Previously, they all worked with UIButtons and IBActions. But since setting up a static tableview, I am running into issues.
I'm trying to link up the individual cells to their own actions and I can't seem to do that with outlets. How might I do this?
Thank you.
Swift 3
Table View code:
import UIKit
class AltSettingsTableViewController: UITableViewController {
#IBOutlet weak var copyrightInfo: UITableViewCell!
#IBOutlet weak var shareApp: UITableViewCell!
#IBOutlet weak var rateApp: UITableViewCell!
#IBOutlet weak var clearCache: UITableViewCell!
#IBOutlet weak var purchasePhotos: UITableViewCell!
#IBOutlet weak var helpFeedback: UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor.clear
self.tableView.backgroundView = nil;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.minimum(35, 35)
}
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
Have you overridden func tableView(UITableView, didSelectRowAt: IndexPath)? It should let you know when a cell is tapped and then you can use indexPath to determine which cell it was.
Edit: documentation for UITableViewDelegate and NSIndexPath.
Example:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
// do stuff
}
//...
}
Related
I have a TableView with cells on my first View, and it works well. But I want to click on some cell then should appear new View with new Table View & cells. But after click, I can see only TableView without any cells.
code of SecondViewController
import UIKit
import WebKit
import Kanna
import Gloss
class SecondViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
#IBOutlet weak var menuTable: SecondViewControllerTableViewCell!
public func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 3
}
public func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_secondPage", for: indexPath) as! ViewControllerTableViewCell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
code of FirstViewController
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
let logos = ["ks", "vf", "ls"]
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return logos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_firstPage", for: indexPath) as! ViewControllerTableViewCell
#code#
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
performSegue(withIdentifier: "segue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
Code of First Table Cell Controller
import UIKit
class ViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var regionQuantity: UILabel!
#IBOutlet weak var localityQuantity: UILabel!
#IBOutlet weak var BSQuantity: UILabel!
#IBOutlet weak var updateDate: UILabel!
#IBOutlet weak var namesOfBS: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Code of Second Table Cell Controller
import UIKit
class SecondViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
And when I tried to print something in
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(3) return 3 } it printed nothing and
2019-01-04 22:54:07.057626+0200 BaseStation[12620:273434] <UIView: 0x7f9f52c1e640; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600001a62a80>>'s window is not equal to <BaseStation.SecondViewController: 0x7f9f52c239e0>'s view's window!
in debuger apeared after click onto cells in first View
I think what you are asking is .. When you click on a cell in your tableview, you navigate to a new view controller. And this new view controller has a tableview that is not being populated with any cells.
Essentially your question is.. Why is your tableview empty.
1) Have you checked if you correctly set the table views datasource?
2) Is the datasource empty?
3) Does your tableview have a visible height/width?
I am working on a todo list app that works with a table view in Swift. The table view has a UILabel called titleLabelOutlet. When I was building the app I got an error that says The titleLabelOutlet outlet from the FirstViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.I have never received this error before. How do I fix it? This is my code:
import UIKit
var phoneNumberList = [String]()
var titleFieldList = [String]()
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var titleLabelOutlet: UILabel!
#IBOutlet weak var phoneNumberLabelOutlet: UILabel!
#IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (phoneNumberList.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
phoneNumberLabelOutlet.text = phoneNumberList[indexPath.row]
titleLabelOutlet.text = titleFieldList[indexPath.row]
return(cell)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == UITableViewCellEditingStyle.delete
{
phoneNumberList.remove(at: indexPath.row)
titleFieldList.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Your outlets should be in cell class instead of viewcontroller because labels are inside cell
And in crllfor row at delegate you have to get your outlets like that
`let cell = tableView.dequeueReusableCell(withReuseIdentifier: cellIdentifier", for: indexPath) as! TableViewCellClass
cell.lbloutlet.text = "any text"
return cell
`
Create CustomTableCell subclassing UITableViewCell.
Have Labels in cell and connect to phoneNumberLabelOutlet & titleLabelOutlet in CustomTableCell class.
use CustomTableCell in place UITableViewCell for cellForRowAtIndex function.
now you can set values for labels as
cell.phoneNumberLabelOutlet.text = phoneNumberList[indexPath.row]
cell.titleLabelOutlet.text = titleFieldList[indexPath.row]
So I added a table view to a normal view controller and inside that table view I made a prototype cell. Ive done some work to make the cell appear but it won't. How do I fix this? Here is my code:
import UIKit
class NewsScreenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var trendingButton: UITabBarItem!
#IBOutlet weak var newestButton: UITabBarItem!
#IBOutlet weak var topJournalists: UITabBarItem!
#IBOutlet weak var tabBar: UITabBar!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
You need to return cell after you get the cell and give it something to display. Try something more like this
let fillTableViewArray = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReuseableCellWithIdentifier("cell")
//now that you have that cell you have to put something into it. For example an Array of strings, can be used to fill the table view
cell!.textLabel?.text = fillTableViewArray[indexPath.row]
return cell!
}
If you want to do this in the storyboard, you can instead add a label to the prototype cell in the storyboard, set an integer value for the label's tag, and then find the label by adding these two lines after you do let cell = ...
let label = cell.viewWithTag(100) as UILabel!
label.text = fillTableViewArray[indexPath.row]
I hope this helps fix your issue
For a project, I've three UITableViews in an UITabbarController. The initial view loads the tableview correctly, but when I tap on the second tab, the table loads the right amount of cells and the right cell class, but don't show the content on it.
I logged the method tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) and every cell gets the right string values.
This is the code I use:
import UIKit
import RealmSwift
class Hapjes: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tabel: UITableView!
let realm = try! Realm()
let productArray = try! Realm().objects(Product).filter("categorie = 1")
override func viewDidLoad() {
super.viewDidLoad()
tabel.dataSource = self
tabel.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Hapjes"
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProductenCellHapjes", forIndexPath: indexPath) as! ProductenCell
var object = productArray[indexPath.row]
cell.label.text = object.valueForKey("productNaam") as! String
cell.plaatje.image = UIImage(named: "1449032338_news.png")
let tmp = object.valueForKey("productNaam") as! String
print("Hapje: \(tmp)")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
ProductenCell.swift:
import UIKit
class ProductenCell: UITableViewCell {
#IBOutlet weak var plaatje: UIImageView!
#IBOutlet weak var label: UILabel!
}
This is the screenshot of the UI how it goes now:
http://i.stack.imgur.com/Gx0Jw.png
Thanks for your help.
I want to create a UIViewController with an embedded UITableView. I've seen various examples but so far have been unable to get it to work. The outlets are embedded in the table cells.
class CREWStartMenuVCCell: UITableViewCell {
#IBOutlet weak var myButton: UIButton!
#IBOutlet weak var myTextView: UITextView!
}
class CREWStartMenuVC: UIViewController { // , UITableViewDelegate { // UITableViewDataSource,
#IBOutlet weak var tableView: UITableView! //<<-- TableView Outlet
override func viewDidLoad() {
super.viewDidLoad()
createAppData()
tableView.registerClass(CREWStartMenuVCCell.self, forCellReuseIdentifier: getCellName(viewName))
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CREWStartMenuVCCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(getCellName(viewName), forIndexPath: indexPath) as! CREWStartMenuVCCell
// processing cell code not relevant
I get this error if I attempt to override the following functions: Method does not override any method from its superclass. What am I missing?
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // was override
// calculate length of text and adjust the size of cell for that
return calculateTipsHeight(viewName, text:getTipsInfo(viewName, sectionNumber: indexPath.section, tipOrder: indexPath.row).tipItemText)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {section))
return getTipsRowCount (viewName, sectionNumber: 0)
}
UPDATE
I get the following errors when I try to add UITableViewDataSource:
5:17: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'
34:10: Candidate has non-matching type '(UITableView!, cellForRowAtIndexPath: NSIndexPath!) -> CREWStartMenuVCCell!'
I don't see the error.
Since the class CREWStartMenuVC does NOT inherit from UITableViewController (or any other class which implements those methods), the methods in the protocols UITableViewDelegate and UITableViewDataSource can (or must) be implemented but they are not overridden. That means that the implementation of those methods cannot be preceded by
override
Also, in case you didn't do it in the Storyboard, don't forget to set the delegate and the dataSource of the tableView, for example, in the call to viewDidLoad() of your CREWStartMenuVC class.
tableView.delegate = self
tableView.dataSource = self
About the two errors from the UPDATE, just replace
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> CREWStartMenuVCCell!
with
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CREWStartMenuVCCell!
(the only differences are the exclamations ! after the classes UITableView and NSIndexPath)
I got it to work without UITableViewDataSource after I removed the tableView.registerClass
Here is the working code:
class CREWStartMenuVCCell: UITableViewCell {
#IBOutlet weak var myButton: UIButton!
#IBOutlet weak var myTextView: UITextView!
}
class CREWStartMenuVC: UIViewController, UITableViewDelegate { //, UITableViewDataSource { //
#IBOutlet var tableView: UITableView! //<<-- TableView Outlet
override func viewDidLoad() {
super.viewDidLoad()
createAppData()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CREWStartMenuVCCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier(getCellName(viewName), forIndexPath: indexPath) as! CREWStartMenuVCCell
// MARK: Common tips code