草庐IT

ios - 多个自定义 UITableViewCell 覆盖彼此 Swift

coder 2023-09-17 原文

我正在尝试创建一个包含多个自定义 UITableViewCell 的 TableView 。如果我只使用一个自定义单元格,则会显示正确的单元格。如果我添加我的第二个单元格,显示第二个单元格会覆盖第一个单元格并禁止交互,第二个单元格会显示第一个自定义单元格的默认数据,直到它被选中然后显示正确的单元格。

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let nib1 = UINib(nibName: "AccountCell1", bundle: nil)
    tableView.registerNib(nib1, forCellReuseIdentifier: "SettingCell1")
    let nib2 = UINib(nibName: "AccountCell2", bundle: nil)
    tableView.registerNib(nib2, forCellReuseIdentifier: "SettingCell2")
    //removing empty rows
    tableView.tableFooterView = UIView()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 2
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    /*let cell = tableView.dequeueReusableCellWithIdentifier( "AccountCell", forIndexPath: indexPath) as! UITableViewCell*/
    let cell1 = tableView.dequeueReusableCellWithIdentifier( "SettingCell1", forIndexPath: indexPath) as! AccountCell1Controller
    let cell2 = tableView.dequeueReusableCellWithIdentifier( "SettingCell2", forIndexPath: indexPath) as! AccountCell2Controller

    // Configure the cell...
    if(indexPath.row == 0)
    {
        let imageName = "Amanda.jpeg"
        let image = UIImage(named: imageName)
        cell1.UserImage.image = image
        cell1.UserLabel.text = "@AmandaSmith"
        cell1.setNeedsLayout()
        return cell1
    }
    else
    {
        return cell2
    }


}
 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if(section == 0)
    {
        return "John Smith"
    }
    else if(section == 1)
    {
        return "Settings"
    }
    else if(section == 2)
    {
        return "About"
    }
    return ""
}
 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(indexPath.row == 0)
    {
        return 204
    }
    else if(indexPath.row == 1)
    {

        return 61
    }
    else {

        return 44
    }
}


}

My Account cell controllers,2是两个按钮对应的 Action

 class AccountCell1Controller: UITableViewCell {
@IBOutlet weak var UserImage: UIImageView!
@IBOutlet weak var UserLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    let user = UserData.self
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state

}

}

最佳答案

问题是即使您没有为该行创建单元格,您也会使 cell1 出队。

尝试只将给定 indexPath.row 实际需要的单元格出队:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  // Configure the cell...

  if(indexPath.row == 0) {
    let cell1 = tableView.dequeueReusableCellWithIdentifier( "SettingCell1", forIndexPath: indexPath) as! AccountCell1Controller
    let imageName = "Amanda.jpeg"
    let image = UIImage(named: imageName)
    cell1.UserImage.image = image
    cell1.UserLabel.text = "@AmandaSmith"
    cell1.setNeedsLayout()
    return cell1
  } else {
    let cell2 = tableView.dequeueReusableCellWithIdentifier( "SettingCell2", forIndexPath: indexPath) as! AccountCell2Controller
    return cell2
  }


}

要理解为什么出列会破坏事物,了解 TableView 的工作原理很重要。

假设您的表格中有 100 个单元格。如果您的设备(例如 iPhone)一次只能显示 10 个,那么创建和管理所有 100 个这些单元将是一种浪费。出队基于这个原则,您只需要分配尽可能多的单元格,因为您将要显示。所以每次你需要一个新的单元格(每次 cellForRowAtIndexPath 被调用)你只想出列你需要的单元格类型,如果一个单元格同时离开屏幕,你可以重用它,而不是删除它并经历(可能)繁重的创建新单元格的过程。

知道这一点后,我仍然不太清楚为什么将两个单元格出队用于单个索引会破坏布局,但它显然混淆了 TableView。作为在单个 tableView 中处理多个自定义单元格时的一般经验法则,最好将 cellForRowAtIndexPath 拆分为每种类型的单元格的单独函数(也可能用于不同的部分,如果这样的话感觉)。这样你就可以在 indexPath.row切换,并根据行调用你自己的自定义 dequeueThisSettingsCelldequeueOtherSettingsCell,将单元格返回到 cellForRowAtIndexPath,然后将其返回到 tableView。这也大大缩短了 cellForRowAtIndexPath,明确了哪种单元格对于哪一行/部分出队,并将每个单元格的设置分成不同的函数(这有助于清晰度和可读性)。

关于ios - 多个自定义 UITableViewCell 覆盖彼此 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34644347/

有关ios - 多个自定义 UITableViewCell 覆盖彼此 Swift的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  7. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

随机推荐