【发布时间】:2020-05-06 03:45:13
【问题描述】:
我正在使用 Swift 5、Xcode 11 和 ui 表视图控制器创建一个简单的应用程序。在 ui 表视图中,我想要 2 个按钮:一个在我的表视图左侧,另一个在右侧。我尝试了许多其他相关/类似问题的答案,但都失败了(可能是因为 1. 太旧,2. 答案用 OBJ-C 编写)。
这是我的表格视图控制器:
import UIKit
@objcMembers class CustomViewController: UITableViewController {
var tag = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tag = tag + 1
let cell = tableView.dequeueReusableCell(withIdentifier: "themeCell", for: indexPath) as! ThemeCell
var cellButton: UIButton!
cellButton = UIButton(frame: CGRect(x: 5, y: 5, width: 50, height: 30))
cell.addSubview(cellButton)
cell.img.image = UIImage(named: SingletonViewController.themes[indexPath.row])
cell.accessoryView = cellButton
cellButton.backgroundColor = UIColor.red
cellButton.tag = tag
return cell
}
}
【问题讨论】:
-
分享你目前收到的截图。
-
为什么不用“themeCell”.xib 文件添加那些按钮?
-
@iOSArchitect.com 查看我的编辑
-
像这样试试 cell.contentView.addSubview(cellButton) 可能有用
-
@RuchiMakadia 这似乎不起作用
标签: ios swift xcode uitableview uicollectionview