2016-07-28 98 views
0

附加您可能会发现(左图)我尝试创建类似(右)UITableView的图片。它们看起来像按钮,但它们只是可以作为tableview单元格点击的单元格。用SWIFT以编程方式自定义UITableViewCell

我已经尝试了许多不同的东西,包括在自定义单元格内添加容器窗口,在自定义单元格内添加UIImage,但我无法复制这些单元格!

我已经尝试过使用自定义单元类,我尝试过通过IB来做它,而我为了我的疯狂,不能重新创建它。

任何人都可以给我一个关于如何创建(内部单元格)文本边框/正方形的提示吗?与不同的背景颜色照明?

如果这可以很容易地用IB来完成,我宁愿这样做,但如果你有一个样本customcell类,我可以看看,这将不胜感激!

failed attempt

感谢您抽出时间来看看我的问题。

回答

2

我已经做了样品为你关闭你的要求。看一看 https://github.com/RajanMaheshwari/CustomTableCell

我想用UITableView来做这个。 我的方法将采取一个自定义单元格,并添加一个UIView与左,右,上下的一些限制。 此外,我将提供相同的背景色为UITableViewUIView这是上海华和单元格内容视图,也使UITableViewseparatorNone和TableCell的选择为None,这样的UI看起来像

enter image description here

enter image description here

enter image description here

接下来将每一个约束,使一个CustomC后并制作IBOutlets,我们将跳转到代码。

我会做所有的阴影,并在自定单元的awakeFromNib方法

概述这将是我CustomTableViewCell

class CustomTableViewCell: UITableViewCell { 

@IBOutlet weak var labelBackgroundView: UIView! 
@IBOutlet weak var cellLabel: UILabel! 
override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 

    labelBackgroundView.layer.borderWidth = 0.5 
    labelBackgroundView.layer.borderColor = UIColor.lightGrayColor().CGColor 
    labelBackgroundView.layer.shadowColor = UIColor.lightGrayColor().CGColor 
    labelBackgroundView.layer.shadowOpacity = 0.8 
    labelBackgroundView.layer.shadowRadius = 5.0 
    labelBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 2.0) 
    labelBackgroundView.layer.masksToBounds = false; 
} 

我有两个出口。

一个是您将在其中显示名称的标签。 其他是你想用一些轮廓和阴影显示的外部视图。

的视图控制器代码将是:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 

var array = [String]() 

@IBOutlet weak var myTableView: UITableView! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    array = ["Wealth","Health","Esteem","Relationship"] 

} 


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell 

    cell.cellLabel.text = array[indexPath.row] 
    cell.labelBackgroundView.tag = indexPath.row 
    cell.labelBackgroundView.userInteractionEnabled = true 

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellViewTapped)) 
    cell.labelBackgroundView.addGestureRecognizer(tapGesture) 

    return cell 
} 


func cellViewTapped(sender:UITapGestureRecognizer) { 

    let view = sender.view 
    let index = view?.tag 
    print(index!) 
    } 
} 

在这里,我没有使用过的UITableViewDelegatedidSelectIndex因为我只想在大纲LabelBackgroundView水龙头,而不是完整的细胞。

所以最终的结局是这样的

enter image description here

+0

这是完美的!我近在咫尺......我在自定义单元格内的容器视图中使用了一个标签......我之前看到的所有内容都是在设计之上的白色空间......无论如何,这在我删除后为我工作我的整个故事板和课程,并从头开始!非常感谢! – Agustin

+0

如果它适合您,请接受为蜱。谢谢 –

+0

我的坏...我以前做过...做过! :) 再次感谢 – Agustin

1

我认为你是在正确的道路上。我在一个项目中做了类似的事情。我创建了UIView的子类(在视图中也添加了一个阴影),并在单元格内添加了一个具有此类型的视图。

class ShadowedView: UIView { 

override func awakeFromNib() { 
    layer.shadowColor = UIColor(red: 157.0/255.0, green: 157.0/255.0, blue: 157.0/255.0, alpha: 0.5).CGColor 
    layer.shadowOpacity = 0.8 
    layer.shadowRadius = 5.0 
    layer.shadowOffset = CGSizeMake(0.0, 2.0) 
} 
} 

不要忘记给单元格内的视图添加一些约束。

+0

有趣的是,每次我把我的“阴影视图”类的东西的时候,(像这样的例子),我会得到这样的白色框由于某种原因覆盖了我的tableview的90%....但是我最终使用了类似于这个的代码以及来自@Rajan Maheshwari的代码。谢谢你们俩! – Agustin

0

你可以安排在“大小insepector”

您的收藏视图布局和小区自定义您的影像。

enter image description here

enter image description here

+0

它看起来更接近我的...但是你使用的是收集视图...我知道它们几乎与表格视图相同,但我宁愿坚持使用tableviews,直到我获得更多经验其他:) – Agustin