Nil*_*ne- 40 cocoa-touch storyboard uitableview swift ios8
我有一个带有几个IBOutlets的自定义单元类.我已将该课程添加到故事板中.我连接了所有的插座.我的cellForRowAtIndexPath函数如下所示:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SwipeableCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义单元类:
class SwipeableCell: UITableViewCell {
@IBOutlet var option1: UIButton
@IBOutlet var option2: UIButton
@IBOutlet var topLayerView : UIView
@IBOutlet var mainTextLabel : UILabel
@IBOutlet var categoryIcon : UIImageView
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我的所有单元格都是空的.我已经注销self.venueService.mainCategoriesArray(),它包含所有正确的字符串.我也试过把一个实际的字符串等于标签,这会产生相同的结果.
我错过了什么?任何帮助表示赞赏.
Sur*_*gch 99
使用Xcode 9和Swift 4进行测试
原问题的提问者已经解决了他们的问题.我将这个答案添加为一个迷你自包含示例项目,供其他人尝试做同样的事情.
完成的项目应如下所示:
它可以只是一个单一视图应用程序.
将新的Swift文件添加到项目中.将其命名为MyCustomCell.swift.此类将保留您在故事板中添加到单元格的视图的出口.
import UIKit
class MyCustomCell: UITableViewCell {
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myCellLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
我们稍后会联系这些商店.
打开ViewController.swift并确保您拥有以下内容:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
// These are the colors of the square views in our table view cells.
// In a real project you might use UIImages.
let colors = [UIColor.blue, UIColor.yellow, UIColor.magenta, UIColor.red, UIColor.brown]
// Don't forget to enter this in IB also
let cellReuseIdentifier = "cell"
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.myView.backgroundColor = self.colors[indexPath.row]
cell.myCellLabel.text = self.animals[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}
}
Run Code Online (Sandbox Code Playgroud)
将表视图添加到视图控制器并使用自动布局将其固定到视图控制器的四个侧面.然后将表视图单元格拖到表视图上.然后将View和Label拖到Prototype单元格上.(您可能需要选择表格视图单元格和手动行高设置的东西的尺寸检查,让你有更多的空间进行操作高.)使用自动布局来解决视图和标签,你怎么想他们之内安排表视图单元格的内容视图.例如,我将我的视图设为100x100.
自定义类名称和标识符
选择Table View Cell并将自定义类设置为MyCustomCell(我们添加的Swift文件中的类的名称).同时将标识符设置为cell(与cellReuseIdentifier上面代码中使用的字符串相同).
连接奥特莱斯
tableView变量ViewController.myView与类中的myCellLabel变量相同的操作MyCustomCell.而已.您应该能够立即运行您的项目.
Shi*_*esh 18
这适用于使用.xib工作的自定义单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let identifier = "Custom"
var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCel
if cell == nil {
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
}return cell}
Run Code Online (Sandbox Code Playgroud)
小智 14
我也有同样的问题.
一般来说,我所做的与你一样.
class dynamicCell: UITableViewCell {
@IBOutlet var testLabel : UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Run Code Online (Sandbox Code Playgroud)
并在uitableviewcell方法中:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell :dynamicCell = tableView.dequeueReusableCellWithIdentifier("cell") as dynamicCell
cell.testLabel.text = "so sad"
println(cell.testLabel)
return cell;
}
Run Code Online (Sandbox Code Playgroud)
是的,tableview什么都没显示!但是猜猜它实际上显示了什么......因为我从println(cell.testLabel)获得的日志显示所有标签实际上都显示出来了.
但!他们的框架很奇怪,有这样的东西:
frame =(0 -21; 42 21);
所以它有一个(0,-21)为(x,y),这意味着标签只出现在单元格边界之外的某个地方.
所以我尝试手动添加调整框架,如下所示:
cell.testLabel.frame = CGRectMake(10,10,42,21)
可悲的是,它不起作用.
--------------- 10分钟后更新-----------------
我做的.所以,似乎问题来自Size Classes.
单击.storyboard文件,然后转到文件检查器选项卡
取消勾选"大小类"复选框
最后我的"伤心"标签出来了!
Nil*_*ne- 11
感谢所有不同的建议,但我终于明白了.自定义类已正确设置.我需要做的就是在故事板中我选择自定义类:删除它,然后再次选择它.它没有多大意义,但最终为我工作.
上次更新的版本是xCode 6.1
class StampInfoTableViewCell: UITableViewCell{
@IBOutlet weak var stampDate: UILabel!
@IBOutlet weak var numberText: UILabel!
override init?(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
142859 次 |
| 最近记录: |