【问题标题】:UITableView Using SwiftUITableView 使用 Swift
【发布时间】:2014-06-05 09:15:41
【问题描述】:

我的TapCell1.swift

这是自定义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)
    }
}

我的 ViewController.swift

它的所有数据源和委托都设置正确。但我的自定义单元格没有显示。

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.
        }
}

问题是我的自定义单元格未显示。请提出我做错的任何事情。

注意:这是我的代码My File Download Link

【问题讨论】:

  • 你确定这条线是正确的吗? self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell") 尝试删除 .self
  • TapCell1.self 等价于 Objective-C 中的 TapCell1.class
  • 好的,我会检查@eXhausted
  • 你用的是板还是笔尖?

标签: ios uitableview swift customization


【解决方案1】:

我终于做到了。

对于 TapCell1.swift

import UIKit

class TapCell1: UITableViewCell {

    @IBOutlet var labelTitle: UILabel

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

对于 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
    }
}

我的工作代码链接:CUSTOMISED TABLE

【讨论】:

  • @MoorthyTheBoss:是*
  • 正确答案来自@Micah:你必须在你的代码中添加tableView:heightForRowAtIndexPath:,这是一个Xcode 6(到目前为止,beta6)的错误。与手头的问题无关。
  • 应该是0...70(三个点不是两个)?
  • Xcode 6.3 中的 Swift 1.2 建议 as 必须是 as!
【解决方案2】:

您应该为该单元注册class。为此 把这行代码改成

self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")

编辑

你的代码看起来不错,我检查了它

//cell.labelText.text="Cell Text"

cell.textLabel.text="Cell Text"   // use like this

【讨论】:

  • 是的,我知道。它已经为我工作了。但我需要显示 TapCell1
  • 现在只显示 TapCell1。你还想要什么
  • TapCell1 颜色为绿色。最后我做到了。感谢您宝贵的时间。
  • 这解决了我的问题 - 但我很困惑,我在 Storyboard 中正确设置了它,那么为什么我还需要在代码中手动注册呢?这与 objc 不同。有什么想法吗?
  • @g_pass 如果您在情节提要中使用原型单元,则无需通过代码再次注册。如果您只使用自定义单元格,您可以通过registerCellregisterNib 注册它
【解决方案3】:

解决方案很可能是手动设置单元格高度:

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

我相信这是 Xcode beta 6 的错误。

【讨论】:

    【解决方案4】:

    我现在已经能够让自定义 UITableViewCell 工作了。

    适用于

    • 在 Xcode 6 beta 6 上运行
    • 在 Xcode 6 beta 5 上运行

    • iOS 是 7.1

    如何

    • 我已经为单元格创建了一个“.xib”文件。
    • 我将其复制到情节提要中。
    • 通过情节提要,我给它一个标识符。
    • 我确保它是 tableview 的子子项

    这样做,你不需要注册类/笔尖等。

    这是我的自定义单元格。

    导入 UIKit 类TestCell:UITableViewCell { @IBOutlet var titleImageView:UIImageView! @IBOutlet var titleLabel:UILabel! 覆盖初始化(样式:UITableViewCellStyle,reuseIdentifier:字符串!){ super.init(风格:风格,reuseIdentifier:reuseIdentifier) } 覆盖 func awakeFromNib() { super.awakeFromNib() // 初始化代码 } 需要初始化(编码器aDecoder:NSCoder){ super.init(编码器:aDecoder) } }

    在您的视图中,或您扩展“UITableViewDataSource”的任何位置。 确保“cell2”与您通过情节提要提供的“标识符”相同。

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell // 使用自定义元素的示例。 cell.titleLabel.text = self.items[indexPath.row] var topImage = UIImage(命名:“fv.png”) cell.titleImageView.image = topImage 返回单元格 }

    【讨论】:

      【解决方案5】:

      检查您的故事板选择单元格并查看“身份检查器,选择 CLASS 输入您的 CustomClass 和 MODULE 输入您的项目名称

      我已经完成了它完美地工作尝试这个提示以避免错误看到下面的图像和代码

      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
      }
      

      【讨论】:

      • 经过 1 小时的疼痛,这对我有所帮助。我认为这个问题与 swift 相关,因为我刚刚开始 swift。但这表明我应该跳出框框思考并首先进行一些基本检查。谢谢。
      【解决方案6】:

      试试下面的代码

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

      https://github.com/iappvk/TableView-Swift

      【讨论】:

        【解决方案7】:

        如果您不使用 Storyboard,则创建一个新文件作为 UITableViewCell 的子类,在设置自定义单元格后选中“也创建 xib 文件”复选框。这是 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!
        
        
        }
        

        【讨论】:

        • -1,它的老方法,你应该在viewDidLoad中使用tableView.registerNib(,forCellReuseIdentifier:)
        【解决方案8】:

        试试下面的代码:

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

        【讨论】:

          【解决方案9】:

          NSOject 的扩展

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

          自定义 TableViewCell 上的属性

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

          在 UIViewController 中添加

          self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)
          
          let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell
          

          【讨论】:

            【解决方案10】:

            这对我来说是一种纯粹的快速符号

            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! 
            }
            

            【讨论】:

              猜你喜欢
              • 2014-10-10
              • 1970-01-01
              • 2016-05-20
              • 2018-10-21
              • 2017-12-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多