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

Identifying the trigger of iOS Remote notifications callback
Linker command failed, cannot find library in existing project
Stop all the functions when changing Viewcontroller
iOS & Parse Push Notification - iPhone 5s/c
Flick a UIScrollView programmatically
Negative Error testing in iOS PayPal Sandbox mode
why i have this showAlert error?
Issue in sending email from iOS device?
iPad crash with Foundation
Reorder Uitableview with Parse Objects?
Running Xamarin.iOS tabbed application in VS2015
How to change presentViewController Transition animation
Overriding 'finished' in asynchronous NSOperation
Change parameter inside dispatch_async from main thread
fb ios sdk 4.5 crashes in iOS7
How to get last frame from video?

Categories

HOME
wpf
document
ffmpeg
collections
copy
design
chronicle
format
windows-10-desktop
xorg
bluej
chart.js2
tapi
reload
android-dialogfragment
watchservice
countdown
appcelerator-alloy
aikau
robotium
slf4j
iteration
esri
coqide
buffer-overflow
ocean
solarwindslem
gcp
swipe
dashboard-designer
nav
jdom-2
ecdsa
partition
cookiestore
windows-scripting
edeliver
gradle-tooling-api
simplesamlphp
jahia
jawr
singlepage
replicate
jeditorpane
lapply
blocking
file-import
main-activity
id3
genetic
jedi
coldfusion-10
par
cocoon-gem
julian
tabbar
equinox
treegrid
vbo
gem
boost-coroutine
openxls
janino
swift-array
mpj-express
laravel-validation
controlsfx
dataformat
magento-1.6
b2b
jqgrid-php
acts-as-taggable-on
lightstreamer
django-endless-pagination
str-to-date
libnids
programmers-notepad
neventstore
sizzle
guvnor
lungojs
self-organizing-maps
expression-encoder-sdk
azure-role-environment
jammit
xmltextwriter
mkmapviewdelegate
seam-conversation
queryanalyzer
coords
iequalitycomparer
sharepoint-feature
roguelike

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