ios


Custom UITableViewCell not appearing


I've got a UITableViewController with a custom cell view. I created an empty Interface Builder document, then added a Table View Cell, then added a label to it. The Table View Cell has a corresponding class that extends UITableViewCell. The Table View Cell's label in Interface Builder is linked (outet) to the var in my custom class
class MyTableViewCell: UITableViewCell {
#IBOutlet var someLabel: UILabel!
The problem is that the the custom cell never renders, it's always blank (I tried the background color trick too). I never see the label. In fact the label is always null.
In my UITableViewController's viewDidLoad(), I've tried
let nib = UINib(nibName: "MyTableCellView", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "myCell")
as well as
tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myCell")
I also have
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)")
return cell
}
At runtime it is dequeueing as cell is non-null, however cell.someLabel is nil.
What does it take to have a custom table view cell render?
someLabel has no value. Try:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
cell.someLabel.text = "Put label text here"
return cell
}
I usually do this way. I load the xib file within the custom table view cell class.
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
xibSetup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
xibSetup()
}
func xibSetup() {
cell = loadViewFromNib()
cell.frame = self.bounds
cell.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(cell)
}
func loadViewFromNib() -> UITableViewCell {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "MyTableViewCell", bundle: bundle)
let cell = nib.instantiateWithOwner(self, options: nil)[0] as! UITableViewCell
return cell
}
}
Along with:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell
print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)")
return cell
}
One other thing to do is to set the File's Owner in MyTableViewCell.xib file to MyTableViewCell class.

Related Links

I want to use same UserInterface with different Data in ios
IQKeyboardManager not working when UITableView embedded in a container view
How to save image from camera/photo library into an array that is to be displayed in a UICollectionView
Adding target to UIControl
Uploading multiple builds of same iOS Application
Swift json delete key
Delay start of animation for UIView with CAKeyframeAnimation?
Why does an NSObject have to be initialized before its' .class-property is readable
Offline mode for Google Maps iOS
What does glGenVertexArraysOES() do and where is the documentation?
dateFromComponents gives Wrong date
ios non-renewing subscription's duration in sandbox
Variable Cells in UITable
“.gif” File loads and then disappears after 1 second
Cannot close InAppBrowser on iPhone, done button is missing when page is opened.
How to delete a polyline in Mapbox iOS SDK?

Categories

HOME
embedded-linux
common-lisp
dom
varnish
anylogic
xcode8
logback
oracle-adf
restsharp
phpmailer
splunk
regression
asp.net-mvc-3
avplayer
sql-server-express
scrollbar
apex
jquery-selectors
cmusphinx
abaqus
hp-alm
powershell-v2.0
azure-ad-graph-api
netcdf
urlrewriter.net
unsigned-integer
physics
simple-html-dom
text-parsing
codewarrior
temp
rdf4j
matterjs
pvlib
codeskulptor
azure-app-service-envrmnt
jackrabbit
draggable
titanium-alloy
runc
windows-scripting
azure-management-api
btle
zuul
rating-system
ewsjavaapi
master-data-management
sshd
view-source
jahia
flying-saucer
replicate
ipmi
django-errors
gwt-material-design
topojson
error-correction
redux-framework
data-fitting
tealeaf
android-location
rras
pddl
id3
metalkit
dual-table
clientid
oracle-bi
par
cng
hjson
nominatim
android-listfragment
nbug
linegraph
crowdflower
solr-boost
generalization
scribe
kue
mpj-express
repaint
citymaps
django-postgresql
httpservice
private-methods
dynamic-typing
clearcase-remote-client
globus-toolkit
notifydatasetchanged
hebrew
tlb
jxta
showcaseview
external-dependencies
semantic-markup
globbing
html5-apps
google-goggles
haar-wavelet
calloc
asp.net-session
ad-hoc-distribution
ekeventkit
force.com
noindex
expressionvisitor
magento-1.5
gemstone
ssao
blackberry-os-v5
jquery-pagination
servletunit
custom-tag
jquery-1.3.2
symstore
xoom
queryanalyzer
gedcom
newsgroup
server.transfer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App