首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编程快捷: TableView在TableView中

编程快捷: TableView在TableView中
EN

Stack Overflow用户
提问于 2022-09-27 16:10:14
回答 1查看 45关注 0票数 0

目前,我正在尝试在tableview中放置一个tableview,但是以编程的方式(为了更好地理解wtf,我正在用故事板的方式这样做)。我现在被困在里面的桌子上了,有什么帮助吗?

代码语言:javascript
复制
class ContactsViewController: UIViewController, UITabBarDelegate, 
 UITableViewDataSource, UITableViewDelegate {

private let contacts = ContactAPI.getData()
let contactsTableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(contactsTableView)
    contactsTableView.translatesAutoresizingMaskIntoConstraints = false // enable Auto-Layout
    
    contactsTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    contactsTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    contactsTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    contactsTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    
    contactsTableView.dataSource = self
    contactsTableView.delegate = self 
    
    contactsTableView.register(ContactsCell.self, forCellReuseIdentifier: "contactCell")
    contactsTableView.rowHeight = 1000
    contactsTableView.estimatedRowHeight = 100

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.contacts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactsCell
    
    cell.jobLabel.text = contacts[indexPath.row].jobTitle
    cell.nameLabel.text = contacts[indexPath.row].name
    
    return cell
}

}

外部TableViewCell由

代码语言:javascript
复制
class ContactsCell: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.addSubview(image)
    contentView.addSubview(nameLabel)
    contentView.addSubview(jobLabel)
    contentView.addSubview(tablello)
    
   
  }

  // working code that defines image, nameLabel, jobLabel
  let tablello: UITableView = {
   
    let tab = UITableView()
    let data = innerTable()
    
    tab.dataSource = data
    tab.delegate = data
    tab.frame = CGRect (x: 0, y: 0, width: 300, height: 300)
    
    tab.register(gInnerCell.self, forCellReuseIdentifier: "gennaroCell")
    tab.translatesAutoresizingMaskIntoConstraints = false
    
    return tab
     }()
    }

和相关类

代码语言:javascript
复制
class innerTable: UIViewController, UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "gennaroCell", for: indexPath) as! gInnerCell
    cell.labelloNapoli.text = "\(indexPath.row)"
    print("AO GENNà")
    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    print("loaded inner")
}



}



class gInnerCell: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

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

    // Configure the view for the selected state
    
    
}

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.addSubview(labelloNapoli)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

let labelloNapoli:UILabel = {
    let lable = UILabel()
    lable.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    return lable
}()
}

我尝试过将tablello转换为单独的视图,并将表添加到那里作为子视图,但是(尽管它打印了inside print() ),它会破坏我的所有UI。

EN

回答 1

Stack Overflow用户

发布于 2022-09-29 18:09:53

通常,通过在另一个tableView中放置一个tableView,您想要实现什么?您能够使用自定义tableViewCell显示相同的信息吗?或者更好的是使用CollectionView,因为它们在UI方面非常灵活。很抱歉没有回答这个问题,只是想更好地理解你想要实现的UI智慧。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73870657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档