I can't make outlet in uitableviewcell - ios

I don't want to make my code messy so I made a separate class for table view cell but when I tried to make outlet of my UIImageView, it's showing error
import UIKit
class TableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
#IBOutlet var image: UIImageView!
}
// code of my tableview
import UIKit
class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
var array = ["apple","blue_graps","cherry","grape","Pineapple","pomegranate","Strawberry"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.imageView?.image = UIImage(named: array[indexPath.row])
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
}

Change the variable image to some other name, e.g. myImage or icon.
There is a deprecated property in UITableViewCell called image and you cannot redeclare it. (It was deprecated in iOS 3 already but it's still there).

Related

UITableView - Not showing

I have set up tableview many times but don't why is not it showing anything. I have added a tableview on my storyboard file, added reference on ViewController file and set datasource and delegate on storyboard. on viewDidLoad registered the cell and finally added reusableIdentifier on TableViewCell. Here is my code with details configuration :
import UIKit
struct ViewControllerConstants {
static let kCellIdentifier = "TableViewCell"
static let kNibName = "TableViewCell"
}
class ViewController: UIViewController {
var cellImageNames : [String] = ["image1","image2","image3","image4","image5","image6"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.register(UINib.init(nibName: ViewControllerConstants.kNibName, bundle: nil), forCellReuseIdentifier: ViewControllerConstants.kCellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
}
}
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellImageNames.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: ViewControllerConstants.kCellIdentifier, for: indexPath) as? TableViewCell {
cell.contentImageView.image = UIImage.init(named: cellImageNames[indexPath.row])
return cell
}
return UITableViewCell.init()
}
}
extension ViewController : UITableViewDelegate {
}
Try to remove identifiers for cells in storyboard.
Problem: Cell height was not defined on code. So it was creating cell with 0 height value .
Solution: Need to specify height of each row as below code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

TableViewCell doesn't show imageView

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.

TableView cellForRowAtIndexPath is never called (iOS, Swift)

I have a problem with creating a table view in UITableViewController. I have tried everything I have found, reuseIdentifier is set correctly, number of rows returns non-zero number and height is set in storyboard. Only the cellForRowAtIndexPath is not called, other table functions runs correctly.
Here is the code:
TableViewController:
import UIKit
import Photos
class CropTableViewController: UITableViewController {
var photos = [Photo]()
var photoAssets = [PHAsset]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return photos.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CropTableViewCell
let photo = photos[indexPath.row]
cell.photoImageView.image = photo.photo
return cell
}
}
TableViewCell:
import UIKit
class CropTableViewCell: UITableViewCell {
#IBOutlet weak var photoImageView: UIImageView!
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
}
}
The photos array is empty, so photos.count is zero, so numberOfRowsInSection returns 0, so the runtime knows that there are no cells and that there is no need to call cellForRowAtIndexPath.

not calling didSelectRowAtIndexPath method in Tableview

I have embedded UITabbar in UIViewController and then I put some UITabbarItems in my UITabbar. I also created another UIViewController with UITableView embedded in it.
I have linked one of the UITabbarItem with this newly created UIViewController(with UITableView in it).
When I run the program and tap on that UITabbarItem it shows a table view but when I select any row of table, the data displaying in UITableView disappears.
I then debug the code and found that didSelectRowAtIndexPath method didn't call.
I searched on the internet and find this but cannot understand it well.
I tried by implementing both types of tableViews (UITableViewController,UIViewController with UITableView in it) but still same problem.
I am stuck here. Anyone please help me. How to get resolve this issue.
I will be very Thankful to you.
Here is my swift code
import UIKit
class DashboardViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var selectedIndexPathForCheckMark:NSIndexPath?
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("MyCell")!UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
selectedIndexPathForCheckMark = indexPath
tableView.reloadData()
}
here are its snapshots
Second UITabbarItem from right implements UITableView
Follow Steps:
1. Take A view controller and add tableview.
2. set outlet with MytabelView
3. set Datasource and delegate of tableview to ViewController
4. and put the following code in ViewController class
5. Take a custom tableview cell with name MyCell having cell identifier "MyCellid"
6. Create swift file with name MyCell, make it subclass of UITableViewCell
7. Choose custom cell and assign it class to MyCell.
I think Its done
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var MytableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.MytableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCellid")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.MytableView.dequeueReusableCellWithIdentifier("MyCellid")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
}

Separating ViewController and UITableViewDataSource

I'm trying to clean up my crowded ViewController by moving the UITableViewDataSource (and Delegate) to a seperate class.
While the DataSource was inside the ViewController (see just below), it worked fine
class ViewController: UIViewController, UITableViewDataSource{
//MARK: Properties
#IBOutlet weak var myTableView: UITableView!
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = self
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
With The myTableViewCell class
class myTableViewCell: UITableViewCell{
#IBOutlet weak var myLabel: UILabel!
override func awakeFromNib()
{super.awakeFromNib()}
override func setSelected(selected: Bool, animated: Bool)
{super.setSelected(selected, animated: animated)}
}
And this works fine and populates a basic table with the labels filled from the strings in myArray.
However when I move the DataSource to it's own class, as follows, it doesn't work
ViewController
class ViewController: UIViewController
{
//MARK: Properties
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
myTableView.dataSource = myDataSource()
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()}
}
and here's the myDataSource class
class myDataSource: NSObject, UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
(The myTableViewCell remains the same)
This setup just outputs an empty table, even though all I did was copy-paste the code from the ViewController to myDataSource. What am I missing? (aside form the delegate. I'll deal with that later, first I need to find what's the problem with the data source).
I'm a bit of a rookie to swift, so I'm having a really rough time understanding where I'm going wrong here. Any help at all would be greatly appreciated. If you could just remember in your answer that I'm just starting out, so try not to throw too many complicated concepts at me without explaining. Thanks
Probably the code in your data source never gets called because your data source doesn't get retained by the table here myTableView.dataSource = myDataSource() so basically gets released right away. To solve this, keep the data source as a property.
Declare the separate class as an extension of the ViewController class
extension ViewController: UITableViewDataSource
{
let cellIdentifier = "myTableViewCell"
let myArray = ["Label one", "Label two", "Label three"]
//MARK: TableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{return 1}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return myArray.count}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! myTableViewCell
cell.myLabel.text = myArray[indexPath.row]
return cell
}
}
I'm using also always delegate methods in an extension declaration but in the same file as the main class

Resources