【发布时间】:2016-01-28 07:41:20
【问题描述】:
我在将我创建的 UIView 放入特定 UITableView 单元格时遇到问题。这是我的代码:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//MARK : Properties
var tableView = UITableView()
var items: [String] = ["Age", "Gender", "Smoking Hx", "Occup. -Ag", "Family Hx", "Chronic Lung Disease Radiology", "Chronic Lung Disease Hx", "Nodule Border", "Nodule Location", "Satellite Lesions", "Nodule Pattern Cavity", "Nodule Size"]
var navigationBar = NavigationBar()
var gender = GenderView()
var maleIcon = MaleIconView()
override func viewDidLoad() {
super.viewDidLoad()
//Create TableView
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width,
self.view.bounds.height - self.view.bounds.height * 0.097);
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
//Create Navigation Bar with custom class
self.navigationBar = NavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.097))
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
//Cell wont turn grey when selected
cell.selectionStyle = UITableViewCellSelectionStyle.None
//Want to add UIView male Icon in first row
if(indexPath.row == 0){
maleIcon = MaleIconView(frame: CGRect(x: 0, y: 0, width: 55, height: 55))
maleIcon.center.x = cell.center.x + cell.center.x * 0.5
maleIcon.center.y = cell.contentView.center.y
maleIcon.layer.cornerRadius = 0.5 * maleIcon.bounds.size.width
//maleIcon.layer.borderWidth = 3.0
cell.addSubview(maleIcon)
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.view.bounds.height * 0.15
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
}
也许问题存在于 MaleIconView() 中?这是代码:
class MaleIconView: UIView {
var maleView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
setUpView()
}
func setUpView(){
maleView.frame = CGRectMake(0, 0, self.bounds.width, self.bounds.height)
maleView.alpha = 1
maleView.backgroundColor = UIColor.blackColor()
maleView.layer.cornerRadius = 0.5 * maleView.bounds.size.width
self.addSubview(maleView)
}
func hide(){
self.removeFromSuperview()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我试图让 UIView 出现在单元格 0 的右侧。还有 indexPath 全部关闭的问题,我认为这可能是 UIView 出现在多个单元格中的一些问题。提前谢谢!
【问题讨论】:
-
试试 cell.contentView.addSubview(maleIcon)
-
@DavidWilliames 不,这似乎不起作用,它产生了相同的结果。我相信我的 IndexPath.row 有问题,因为当我打印它时,即使它是第 12 行,最后一行也会吐出“5”。但我觉得我的编程是标准的,我没有做任何真正不同的事情。
-
@DavidWilliames UIView 目前正在出现,它只是出现在多行中,有时会重新出现在自身之上。
-
哦,对了。那是因为你永远不会从视图/单元格中删除它,所以当带有图标的单元格被出列和重用时,它会继续出现在单元格位于第 0 行某一点的每个实例中。这有意义吗?跨度>
-
另一个快速观察是,您似乎正在根据“项目”数组中的值创建一个带有静态单元格的表格视图。也许看看这个 repo github.com/venmo/Static 它对静态表视图非常有用。 :)
标签: ios swift uitableview uiview