Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 463 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UITableView Using Swift

#1
My **TapCell1.swift**

This is Custom `UITableViewCell View`


import UIKit

class TapCell1: UITableViewCell
{
@IBOutlet var labelText : UILabel

init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
println("Ente")
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}

override func setSelected(selected: Bool, animated: Bool)
{

super.setSelected(selected, animated: animated)
}
}

My **ViewController.swift**

Its All DataSource and Delegates are set correctly.But My custom Cell is not displaying.





import UIKit

class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var label: UILabel

@IBOutlet var tableView : UITableView

var getvalue = NSString()

override func viewDidLoad()
{
super.viewDidLoad()

label.text="HELLO GOLD"

println("hello : \(getvalue)")
self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell")
}

func tableView(tableView:UITableView!, numberOfRowsInSection section:Int)->Int
{
return 5
}

func numberOfSectionsInTableView(tableView:UITableView!)->Int
{
return 1
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1
cell.labelText.text="Cell Text"
return cell
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}




The Problem is My custom cell is Not displayed. Please suggest anything i did wrong.


**Note: here is My code [My File Download Link][1]**


[1]:

[To see links please register here]

Reply

#2
**Extention of NSOject**

extension NSObject {
var name: String {
return String(describing: type(of: self))
}
class var name: String {
return String(describing: self)
}
}

**Properties on Custom TableViewCell**

class var nib: UINib {
return UINib(nibName:YourTableViewCell.name, bundle: nil)
}
class var idetifier: String {
return YourTableViewCell.name
}

**Add in UIViewController**

self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)

let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell
Reply

#3
Try this following code:

var cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellTableView") as? CustomCellTableView
Reply

#4
I finally did it.

**For TapCell1.swift**

import UIKit

class TapCell1: UITableViewCell {

@IBOutlet var labelTitle: UILabel

init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}
}

**For NextViewController.swift**

import UIKit

class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView

var ListArray=NSMutableArray()

override func viewDidLoad() {
super.viewDidLoad()

let nibName = UINib(nibName: "TapCell1", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "Cell")

for i in 0...70 {
ListArray .addObject("Content: \(i)")
}
}


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)->Int {
return ListArray.count
}

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 51
}

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1

//cell.titleLabel.text = "\(ListArray.objectAtIndex(indexPath.item))"

cell.labelTitle.text = "\(ListArray.objectAtIndex(indexPath.row))"

return cell
}
}

My working code link: [CUSTOMISED TABLE][1]

[1]:

[To see links please register here]

Reply

#5
It is Purely swift notation an working for me

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cellIdentifier:String = "CustomFields"
var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
if (cell == nil)
{
var nib:Array = NSBundle.mainBundle().loadNibNamed("CustomCell", owner: self, options: nil) cell = nib[0] as? CustomCell
}
return cell!
}
Reply

#6
If you are not using Storyboard then create a new file as subclass of UITableViewCell check the checkbox "also create xib files" after that set your custom cell .and here is the code for tableview

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

var customcell:CustomTableViewCellClass? = tableView.dequeueReusableCellWithIdentifier("cell") as? CustomTableViewCellClass
if (customcell==nil)
{
var nib:NSArray=NSBundle.mainBundle().loadNibNamed("CustomTableViewCellClass", owner: self, options: nil)

customcell = nib.objectAtIndex(0) as? CustomTableViewCell
}

return customcell!


}

Reply

#7

I have now been able to get Custom UITableViewCell to work.

# Works on

- Runs on Xcode 6 beta 6
- Runs on Xcode 6 beta 5

- iOS is 7.1

# How
- I have created a ".xib" file for the cell.
- I copy it into the storyboard.
- Via the storyboard I give it a Identifier.
- I make sure it is a sub child of a tableview

Doing it this way, you do not need to register a class / nib etc.

This is my custom cell.
<pre>
import UIKit

class TestCell: UITableViewCell {

@IBOutlet var titleImageView: UIImageView!
@IBOutlet var titleLabel: UILabel!

override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
</pre>

In your view, or where ever you extend "UITableViewDataSource".
Make sure "cell2" is the same as the "Identifier" that you gave it via the storyboard.

<pre>
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell

// Example of using the custom elements.
cell.titleLabel.text = self.items[indexPath.row]

var topImage = UIImage(named: "fv.png")
cell.titleImageView.image = topImage

return cell
}
</pre>

[tag:UITableViewCell]
Reply

#8
Try this following code

`var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell`

[To see links please register here]

Reply

#9
The solution is most likely pinpointed to setting the cell height manually as such:

override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return 44
}

I believe it's an Xcode beta 6 bug.
Reply

#10
Check your Story board select the cell and look at the "identity inspector in that select CLASS type your CustomClass and MODULE type your project name

I have done this It works perfectly try this tip to avoid error to see below image and code


override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

// let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)

// cell.textLabel.text = array[indexPath!.row] as String

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell

cell.nameLabel.text = array[indexPath!.row] as String
cell.RestaurentView.image = UIImage(named:images[indexPath!.row])

return cell
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through