我有一个自定义UITableViewCell
,其功能可以向右滑动。滑动手势基于x轴的平移,因此,当x的平移超过40点时,我想触发一个segue。
我认为这是使用委托传递有关当前X值的数据的理想场所。所以,我创建了一个protocol
函数didSwipeCell()
,但我不确定如何将当前x值传递给UITableView
。
请告诉我怎么做,如果您需要任何额外的信息,请在评论中告诉我而不是低估。
答案 0 :(得分:1)
添加x值作为didSwipeCell()方法的参数
protocol TableViewCellDelegate {
func didSwipeCell(on xValue: Float)
}
将委托实例添加到您的单元格
class TableViewCell: UITableViewCell {
weak var delegate: TableViewCellDelegate?
}
然后,当用户滑动提供xValue
的单元格时调用该方法delegate?.didSwipeCell(on: xValue)
在你的UITableView实现TableViewCellDelegate方法。
class TableView: UITableView, TableViewCellDelegate {
func didSwipeCell(on xValue: Float) {
//here you can get the xValue
}
并且,不要忘记在cellForRowAtIndexPath方法中设置TableViewCell的委托
cell.delegate = self
答案 1 :(得分:0)
您只需从 UItableViewCell 类到 UIViewController 即可获得x的值。声明关闭
export class MapComponent implements AfterViewInit {
constructor(private _sanitizer: DomSanitizer){}
private _popUpContent: string = '"Facility" + "<br/>" + "<a routerLink='/view2'>" + "View Two" + "</a>"';
private htmlProperty(): SafeHtml {
return this._sanitizer.bypassSecurityTrustHtml(this._popUpContent);
}
ngAfterViewInit() {
let safeLink = this.htmlProperty();
let openmap = L.tileLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}", {
attribution: 'terms and feedback'
});
let map = L.map("map", {
center: [33.2148, -97.1331],
zoom: 5,
zoomControl: true,
maxZoom: 18
}).addLayer(openmap);
let marker = L.marker([39.2148, -98.1331]).addTo(map);
let popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent(safeLink) //throws type error
.openOn(map);
}
map.on('click', onMapClick);
}
}
在 UItableViewCell 类中,并将其实现到 UIViewController 类中。
在ViewController中
var getValue: ((_ xValue: CGFloat)->())!
答案 2 :(得分:0)
Here is another way to do it...
class SwipeTableViewCell: UITableViewCell {
var didSwipeAction : ((CGFloat)->())?
var startLocation = CGPoint.zero
var theSwipeGR: UISwipeGestureRecognizer?
func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
if let swipe = sender as? UISwipeGestureRecognizer {
if (swipe.state == UIGestureRecognizerState.began) {
startLocation = swipe.location(in: self.contentView);
} else if (swipe.state == UIGestureRecognizerState.ended) {
let stopLocation = swipe.location(in: self.contentView);
let dx = stopLocation.x - startLocation.x;
if dx > 40 {
// pass the ending X coordinate in the callback
didSwipeAction?(stopLocation.x)
}
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
myInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myInit()
}
func myInit() -> Void {
if theSwipeGR == nil {
let g = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
g.direction = UISwipeGestureRecognizerDirection.right
self.contentView.addGestureRecognizer(g)
theSwipeGR = g
}
}
}
and then your table view cell setup becomes:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeCell", for: indexPath) as! SwipeTableViewCell
cell.didSwipeAction = {
(xValue) in
print("Simple", indexPath, "X =", xValue)
// call performSegue() or do something else...
}
return cell
}