我在自定义UIView
中添加了UITableView
,或者更像是一个可用性栏。我有两个问题:
1.-只有当新细胞出现在屏幕上而不是在发布时才会显示视图,如下面的GIF所示。
2.-当多次滚动tableView
时,每个单元格上的UIView
会重新绘制,因此它们会相互重叠
UITableViewCell代码:
import UIKit
class CustomTableViewcell: UITableViewCell {
@IBOutlet weak var bikeStationLabel: UILabel!
@IBOutlet weak var progressView: UIView!
@IBOutlet weak var distanceLabel: UILabel!
override internal func awakeFromNib() {
}
}
我认为没有错,这很简单。我认为我的问题是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
方法。到目前为止我所拥有的是:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomTableViewcell! = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewcell
cell.backgroundColor = UIColor(red: 120/255, green: 120/255, blue: 120/255, alpha: 0.5)
// Dump the data into a single variable to shorten the code
let stations = parsedData[indexPath.row]
// Draw a rectangle on each progress bar depending on the availability of the bikes
let availabilityGraph = UIBezierPath(roundedRect: CGRect(x: cell.progressView.frame.minX, y: cell.progressView.frame.minY, width: CGFloat(Float(stations.freeBikes!) / (Float(stations.freeBikes!) + Float(stations.freeDocks!))) * cell.progressView.frame.width, height: cell.progressView.frame.height), cornerRadius: 0)
let shapeLayer = CAShapeLayer()
shapeLayer.path = availabilityGraph.cgPath
//change the fill color
shapeLayer.fillColor = UIColor.clear().cgColor
//you can change the line width
shapeLayer.lineWidth = cell.frame.height
shapeLayer.strokeColor = UIColor(red: 96/255, green: 96/255, blue: 96/255, alpha: 0.8).cgColor
cell.progressView.layer.addSublayer(shapeLayer)
cell.bikeStationLabel.textColor = UIColor.white()
cell.bikeStationLabel.text = stations.stationName!
cell.distanceLabel.text = "100 m"
return cell
}
答案 0 :(得分:2)
首先提供您的图层特定名称,如此
shapeLayer.name = "ShapeLayer"
现在,在添加progressView
之前,progressView
检查该图层是否已添加到if let sublayers = cell.progressView.layer.sublayers {
for layer: CALayer in sublayers {
if (layer.name == "ShapeLayer") {
layer.removeFromSuperlayer()
break
}
}
}
//Now add the layer to `progressView`
cell.progressView.layer.addSublayer(shapeLayer)
中,如果已添加,则只需删除它。
using Foundation;
using System;
using System.Collections.Generic;
using UIKit;
namespace Blogger
{
public partial class TableViewController : UITableView
{
public List<String> Tests { get; set; }
public TableViewController(IntPtr handle) : base(handle)
{
Tests.Add("Jörg");
Tests.Add("Carsten");
}
[Export("numberOfSectionsInTableView:")]
public int NumberOfSections(UITableView tableView)
{
return 1;
}
[Export("tableView:numberOfRowsInSection:")]
public nint RowsInSection(UITableView tableview, nint section)
{
return (nint)Tests.Count;
}
[Export("tableView:titleForHeaderInSection:")]
public string TitleForHeader(UITableView tableView, int section)
{
return "Header";
}
[Export("tableView:didSelectRowAtIndexPath:")]
public void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
}
[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = new UITableViewCell();
cell.TextLabel.Text = Tests[indexPath.Row];
return cell;
}
}
}