【发布时间】:2016-03-12 11:42:55
【问题描述】:
这是我在UIViewController 中使用自定义 UITableViewCell RunningTableViewCell 的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! RunningTableViewCell
//.......
cell.isTop = false
if(indexPath.row == 0){
cell.isTop = true
}
cell.isBottom = false
if(indexPath.row == myArray.count-1){
cell.isBottom = true
}
return cell
}
这是我的RunningTableViewCell 课程:(Cell 的 GUI 是在情节提要中制作的)
class RunningTableViewCell: UITableViewCell {
//@IBOutlet ...
@IBOutlet weak var myButton: SomeButton!
var isTop: Bool?
var isBottom: Bool?
override func awakeFromNib() {
super.awakeFromNib()
print("result: \(self.isTop) \(self.isBottom)")
myButton.isTop = self.isTop
myButton.isBottom = self.isBottom
}
}
它返回result: nil nil
结果的用法是:(SomeButton是RunningTableViewCell里面的子视图)
class SomeButton: UIButton {
var isTop = false
var isBottom = false
override func drawRect(rect: CGRect) {
if(isTop){
// DO SOMETHING...
}
if(isBottom){
// DO SOMETHING...
}
}
}
那么如何将数据传递给 RunningTableViewCell?
【问题讨论】:
-
有几种方法可以做,比如你设置单元格的标签来识别这个单元格是最后一个还是第一个。
-
@ilesh 如果我想传递超过 1 个数据怎么办? :)
-
它返回 "result: nil nil" 因为在您设置
cellForRowAtIndexPath中的值之前调用了awakeFromNib。 -
@pbasdf 那么我应该在哪里调用以获得
cellForRowAtIndexPath中设置的值? -
@Arefly 这取决于您想要这些值的位置/时间/原因。你能解释更多你想做的事情吗?
标签: ios swift uiviewcontroller uitableview