iOS使用drawrect在tableView内绘制图像
问题描述:
我有一个具有customtableView Cell的UITableView。这已被分类。 tableViewCell有一个图像,标签和一个UIView(见附件截图)iOS使用drawrect在tableView内绘制图像
为了让我把UIView放在单元格内。我使用drawRect在单独的类makeLine.swift中创建了该行,并将其指定为视图的类。
问题1:我想根据TableRownumber更改线的颜色。我已经为customCell类中的UIView做了一个插座。我猜一些需要在cellForRowIndexPath方法中完成 - 但不知道是什么和如何。我不希望使用现有的图像为线
下面是拍行的代码:2一旦这条线是由,我想让它tapable
class LoginLine1: UIView {
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 2.0)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [107.0/255.0, 107.0/255.0, 107.0/255.0, 1.0]
let color = CGColorCreate(colorSpace, components)
CGContextSetStrokeColorWithColor(context, color)
CGContextMoveToPoint(context, 0, 0)
CGContextAddLineToPoint(context, 70, 0)
CGContextStrokePath(context)
}
}
问题和延续到下一个viewController。我已经将tapGestureRecognizer添加到UIView(它具有该行)。我也启用了用户交互。 Tap功能的是下面的 - 但不知何故,它不工作
@IBAction func lineTap(sender: AnyObject) {
performSegueWithIdentifier("segue", sender: self)
}
答
1期
您可以在相应的行数存储在自定义单元格的属性,像这样:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
var cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as CustomCell
cell.rowNumber = indexPath.row
...
return cell
}
在自定义单元格中,为属性创建自定义设置器,如下所示:
var rowNumber : Int {
didSet {
drawLineWithRowNumber(rowNumber)
}
}
drawLineWithRowNumber(rowNumber)
是调用行类的drawRect
方法的方法。这样,您可以将行号传递给负责绘制线条的视图。您仍然必须实施任何逻辑来根据给定的行号选取线条颜色。
第2期
不是使线可点击,你就能更好地运用func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
导航到下一个视图控制器,除非你需要特定行为时,该线路将被挖掘出来。