I am using Xcode 7 Beta, Swift 2
I have a table view with a custom tableViewCell. In the custom cell there is a UIImageView and Label. I have defined a testCell class for this tableViewCell as well. On the image (within the table) I have added UITapRecogniser. I have also enabled User Interaction
Issue: Currently I have 3 rows in the table. When I click on images on first 2 rows. Nothing happens. When I click on the image in the last row - the action prints "DEF" on the console. This has got nothing to with number of rows - problem persists even after changing to 4, 5 or anything else. Basically only the image in the last row gets tapped. Not sure why?? Below is the code:
//Defining custom class for the TableViewCell
class testCell : UITableViewCell{
#IBOutlet weak var testLabel: UILabel!
#IBOutlet weak var testImage: UIImageView!
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! testCell
cell.testLabel?.text = "ABC"
cell.testImage?.image = UIImage(named: "Santa")
return cell
}
#IBOutlet weak var tableView: UITableView!
//TapGestureRecogniser Function
#IBAction func imageTap(sender: AnyObject) {
print("DEF")
}
move the add gesture recognizer call from testCell to cellforrowatindexpath.
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! testCell
cell.testLabel?.text = "ABC"
cell.testImage?.image = UIImage(named: "Santa")
let cellTapRecognizer = UITapGestureRecognizer(target: self, action:Selector("imageTap:"))
cellTapRecognizer.cancelsTouchesInView = false
cell.testImage?.addGestureRecognizer(cellTapRecognizer)
Related
I have a TableView and for it I created a cell in xib. This cell has a label and a button. I would like to display the label and hide the button under certain conditions for the cell I need. How can i do this?
extension SourcesViewController: UITableViewDelegate, UITableViewDataSource {
...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: SourceCell.reuseId, for: indexPath) as! SourceCell
...
return cell
}
class SourceCell: UITableViewCell {
...
#IBOutlet var logInLabel: UILabel!
#IBOutlet var logOutButton: UIButton!
...
#IBAction func logInLogOutButton(_ sender: Any) {
//When I press this button I want to hide log out button and show label for current cell
}
}
My Custom Cell
import Foundation
import UIKit
class MyCustomCell : UITableViewCell {
#IBOutlet weak var myCustomLabel : UILabel!
#IBOutlet weak var myCustomButton : UIButton!
#IBAction func buttonClicked(_ sender : UIButton) {
sender.isHidden = true
}
}
TableView :
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! MyCustomCell
cell.myCustomLabel.text = "Row \(indexPath.row)"
// Configure the cell...
return cell
}
Here is what the output looks like :
I added cell to my tableView and i want to show image in any cell i added an array of image to my class "MathViewController" and i want show these images in per cell.but no image show in any of cell.Do any one know about my problem? I'm very new to swift if you help me it will be great.
its my class MathTableViewCell to get the image from story board:
import UIKit
class MathTableViewCell: UITableViewCell{
#IBOutlet weak var imageCell: UIImageView!
}
and here is the class MathViewController:
import UIKit
class MathViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableController: UITableView!
let items = ["Calculatorimg" , "ss"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableController.register(UITableViewCell.self, forCellReuseIdentifier: "MathTableViewCell")
tableController.rowHeight = UITableViewAutomaticDimension
tableController.estimatedRowHeight = 44
tableController.delegate = self
tableController.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "MathTableViewCell", for: indexPath) as? MathTableViewCell{
cell.imageCell.image = UIImage(named: items[indexPath.row])
return cell
}
return UITableViewCell()
}
}
and here is my storyboard as you cell identifier has been set to MathTableViewCell:
storyboard
Part 1: If create new Xib for cell.
Whole code is correct but forgot to register UITableViewCell Nib file into tableView.
Add below code in ViewDidLoad
tableController.register(UINib(nibName: "MathTableViewCell", bundle: nil), forCellReuseIdentifier: "MathTableViewCell")
Part 2: Cross Verify CellReuseIdentifier is correct.
I have a UICollection view that has two custom cells. The UICollectionViewCell custom cells are built using NIB's. Inside one of the UICollectionViewCell NIB's, I have a UITableView. However, the UITableView itself has custom cells. So my question is:
How do I load a custom cell for the UITableView instead a UICollectionViewCell NIB?
import UIKit
class HeaderFooterCollectionViewCell: UICollectionViewCell {
//Tableview
#IBOutlet weak var tableView: UITableView!
//Buttons
#IBOutlet weak var resetButton: UIButton!
#IBOutlet weak var periodButton: UIButton!
#IBOutlet weak var undoButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// Register cell classes
tableView.register(UINib(nibName: "ShotInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
}
}
extension HeaderFooterCollectionViewCell: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShotInformationTableViewCell
return cell
}
}
let cell = Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name
In almost all cases , XIB_File_Name == XIB_Class_Name
Im using JTAppleCalendar -> https://github.com/patchthecode/JTAppleCalendar Calendar plugin and I want to place ColectionView Cells inside TableView cell , and I want to show there each cell inside added events with title (inside tableview cell) , but when I connecting outlets gives me;
error: Illegal Configuration: The tableView outlet from the ViewController to the UITableView is invalid. Outlets cannot be connected to repeating content.
How can I fix it ?
CellView.swift
import JTAppleCalendar
class CellView: JTAppleCell {
#IBOutlet var selectedView: UIView!
#IBOutlet var dayLabel: UILabel!
#IBOutlet var Label: UILabel!
}
Cell.Swift
import UIKit
class Cell: UITableViewCell {
#IBOutlet weak var label : UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
ViewController.Swift
#IBOutlet weak var tableView: UITableView!
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
cell.label?.text = String(indexPath.row)
return cell
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
My Screen under below;
You need to create custom class of CollectionViewCell and take IBOutlets on it. now you have taken customCell's views IBOutlets in ViewController's class.
OR
Using tag property you can link that view with cell. as below in cellForItemAtIndexPath.
let label:UILabel = cell.viewWithTag(101) as! UILabel // 101 tag need to set in storyboard related view
How to assign tag to view
https://stackoverflow.com/a/31782616/3901620
I have images in my tableView cells, so I have set the cell selectionStyle to None, in order to avoid the cells highlighting when they're selected.
I'm now trying to implement editing into the tableView, allowing users to select multiple cells, filling in the checkmark cirle on the left of the cells. However, this doesn't seem to work with a selectionStyle of None - the circle just remains unfilled.
Is there any way to solve this?
Thanks?
If you can't select the cells directly you can place a uibutton in the cell and recognize in which cell the button that was tapped was.To do that you can set the button tag to equal the indexPath.row inside the cellForRowAtIndexPath method.
The code below prints the correct row number. Hope this helps.
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var onButton: UIButton!
}
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func buttonClicked(sender:UIButton) {
let buttonRow = sender.tag
print(buttonRow)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyTableViewCell
cell.onButton.tag = indexPath.row
cell.onButton.addTarget(self, action: #selector(ViewController.buttonClicked(sender:)), for: .touchUpInside)
return cell
}}