最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】swift中实现列表左滑出现编辑等其他按钮

iOS crifan 769浏览 0评论

想要实现列表,左滑,对应的cell内容向左移动,然后右边显示出对应的三个编辑的按钮:

swift tableview scroll left

swipeable table view lib

uitableview – IOS – How to create TabelView with horizontal scrollable lists – Stack Overflow

How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views

uitableview – Swipe to Delete and the "More" button (like in Mail app on iOS 7) – Stack Overflow

uitableview – Swipe-able Table View Cell in iOS9 or Swift Guide At Least? – Stack Overflow

alikaragoz/MCSwipeTableViewCell: :point_up_2: Convenient UITableViewCell subclass that implements a swippable content to trigger actions (similar to the Mailbox app).

MortimerGoro/MGSwipeTableCell: An easy to use UITableViewCell subclass that allows to display swippable buttons with a variety of transitions.

CEWendel/SWTableViewCell: An easy-to-use UITableViewCell subclass that implements a swippable content view which exposes utility buttons (similar to iOS 7 Mail Application)

runmad/RMSwipeTableViewCell: RMSwipeTableViewCell is a drop-in UITableViewCell subclass that supports pan gestures as seen in apps such as Clear, Mailbox, Sparrow and many more.

看了半天,觉得这个不错:

MortimerGoro/MGSwipeTableCell: An easy to use UITableViewCell subclass that allows to display swippable buttons with a variety of transitions.

Cartfile中添加:

github "MortimerGoro/MGSwipeTableCell"

再去update:

licrifandeMacBook-Pro:SalesAppiOS crifan$ carthage update MGSwipeTableCell

*** Cloning MGSwipeTableCell

*** Fetching SwiftHEXColors

*** Fetching Charts

*** Fetching SwiftKeychainWrapper

*** Fetching Alamofire

*** Fetching XCGLogger

*** Fetching realm-cocoa

*** Fetching Cartography

*** Checking out MGSwipeTableCell at "1.5.5"

*** xcodebuild output can be found in /var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/carthage-xcodebuild.zYFlrX.log

*** Building scheme "MGSwipeTableCell" in MGSwipeTableCell.xcodeproj

后来用对应的代码:

//

//  CustomerTableViewCell.swift

//  SalesApp

//

//  Created by licrifan on 16/5/24.

//  Copyright © 2016年 licrifan. All rights reserved.

//

import UIKit

import Cartography

import MGSwipeTableCell

let CustomerTableViewCellId:String = "CustomerTableViewCellId"

let CustomerAllPaddingX:CGFloat = 10

let CustomerNameWidth:CGFloat = 55

let CustomerModelWidth:CGFloat = 35

let CustomerSourceWidth:CGFloat = 40

let CustomerCurStateWidth:CGFloat = 20

let CustomerLevelWidth:CGFloat = 20

let CustomerUpdateTimeWidth:CGFloat = 40

//let CustomerUpdateTimePaddingRight:CGFloat = 4

let CustomerUpdateTimePaddingRight:CGFloat = CustomerAllPaddingX

//class CustomerTableViewCell: UITableViewCell {

class CustomerTableViewCell: MGSwipeTableCell {

    var customerItem:CustomerItem

    var nameLabel:UILabel

    var modelLabel:UILabel

    var sourceLabel:UILabel

    var curStateImageView:UIImageView

    var levelLabel:UILabel

    var starImageView:UIImageView

    var updateTimeLabel:UILabel

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        customerItem = CustomerItem()

        nameLabel = UILabel()

        modelLabel = UILabel()

        sourceLabel = UILabel()

        curStateImageView = UIImageView()

        levelLabel = UILabel()

        starImageView = UIImageView()

        updateTimeLabel = UILabel()

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCellSelectionStyle.None

        self.imageView?.image = nil

        self.imageView?.hidden = true

        self.textLabel?.text = nil

        self.textLabel?.hidden = true

        self.detailTextLabel?.text = nil

        self.detailTextLabel?.hidden = true

        self.accessoryType = UITableViewCellAccessoryType.None

        self.accessoryView = nil

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    convenience init(curCustomerItem:CustomerItem, reuseIdentifier:String?){

        self.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier)

        self.customerItem = curCustomerItem

        self.contentView.backgroundColor = UIColor.whiteColor()

        self.nameLabel.text = self.customerItem.name

        self.nameLabel.font = UIFont.systemFontOfSize(18)

        self.contentView.addSubview(self.nameLabel)

        constrain(self.nameLabel){nameLabel in

            nameLabel.centerY == nameLabel.superview!.centerY

            nameLabel.left == nameLabel.superview!.left + CustomerAllPaddingX

            nameLabel.width == CustomerNameWidth

        }

        self.modelLabel.text = self.customerItem.model

        self.modelLabel.font = UIFont.systemFontOfSize(12)

        self.contentView.addSubview(self.modelLabel)

        constrain(self.modelLabel, self.nameLabel){modelLabel, nameLabel in

            modelLabel.centerY == modelLabel.superview!.centerY

            modelLabel.left == nameLabel.right + CustomerAllPaddingX

            modelLabel.width == CustomerModelWidth

        }

        self.sourceLabel.text = self.customerItem.source

        self.sourceLabel.font = UIFont.systemFontOfSize(14)

        self.contentView.addSubview(self.sourceLabel)

        constrain(self.sourceLabel, self.modelLabel){sourceLabel, modelLabel in

            sourceLabel.centerY == sourceLabel.superview!.centerY

            sourceLabel.left == modelLabel.right + CustomerAllPaddingX

            sourceLabel.width == CustomerSourceWidth

        }

//        let stateLabel:UILabel = UILabel()

//        stateLabel.text = self.customerItem.curState.rawValue

//        let stateImage = drawRectangleImageWithLabel(CGSizeMake(20, 20), color: UIColor.clearColor(), label: stateLabel)

        self.curStateImageView.image = self.customerItem.curState.image

        self.contentView.addSubview(self.curStateImageView)

        constrain(self.curStateImageView, self.sourceLabel){curStateImageView, sourceLabel in

            curStateImageView.centerY == curStateImageView.superview!.centerY

            curStateImageView.left == sourceLabel.right + CustomerAllPaddingX

            curStateImageView.width == CustomerCurStateWidth

        }

        self.levelLabel.text = self.customerItem.cursomterLevel.rawValue

        self.levelLabel.textColor = self.customerItem.cursomterLevel.color

        self.levelLabel.font = UIFont.systemFontOfSize(16)

        self.contentView.addSubview(self.levelLabel)

        constrain(self.levelLabel, self.curStateImageView){levelLabel, curStateImageView in

            levelLabel.centerY == levelLabel.superview!.centerY

            levelLabel.left == curStateImageView.right + CustomerAllPaddingX

            levelLabel.width == CustomerLevelWidth

        }

        if self.customerItem.isStar {

            self.starImageView.image = UIImage(named: "customer_isStar")

            self.contentView.addSubview(self.starImageView)

            constrain(self.starImageView, self.levelLabel){starImageView, levelLabel in

                starImageView.centerY == starImageView.superview!.centerY

                starImageView.left == levelLabel.right + CustomerAllPaddingX

                starImageView.width == self.starImageView.image!.size.width

            }

        } else {

            self.starImageView.hidden = true

        }

        //self.updateTimeLabel.text = self.customerItem.updateTime.toString("")

        self.updateTimeLabel.text = "3天前"

        self.updateTimeLabel.textColor = UIColor.darkGrayColor()

        self.updateTimeLabel.font = UIFont.systemFontOfSize(13)

        self.contentView.addSubview(self.updateTimeLabel)

        constrain(self.updateTimeLabel){updateTimeLabel in

            updateTimeLabel.centerY == updateTimeLabel.superview!.centerY

            updateTimeLabel.right == updateTimeLabel.superview!.right – CustomerUpdateTimePaddingRight

            updateTimeLabel.width == CustomerUpdateTimeWidth

        }

        //add right buttons

        //let editButton = MGSwipeButton(title: "编辑", backgroundColor: UIColor(hexString: "#F73C38"))

        let editButton = MGSwipeButton(title: "编辑", backgroundColor: UIColor(hexString: "#F73C38"), callback: {

            (sender: MGSwipeTableCell!) -> Bool in

            gLog.verbose("sender=\(sender), customerItem=\(self.customerItem)")

            return true

        })

        let sendSmsButton = MGSwipeButton(title: "短信", backgroundColor: UIColor(hexString: "#FDA529"), callback: {

            (sender: MGSwipeTableCell!) -> Bool in

            gLog.verbose("sender=\(sender), customerItem=\(self.customerItem)")

            return true

        })

        let phoneCallButton = MGSwipeButton(title: "电话", backgroundColor: UIColor(hexString: "#C7C7C7"), callback: {

            (sender: MGSwipeTableCell!) -> Bool in

            gLog.verbose("sender=\(sender), customerItem=\(self.customerItem)")

            return true

        })

        self.rightButtons = [editButton, sendSmsButton, phoneCallButton]

        self.rightSwipeSettings.transition = .Rotate3D

    }

}

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let curCustomerItem = self.customerItemList[indexPath.row]

        let cell = CustomerTableViewCell(curCustomerItem: curCustomerItem, reuseIdentifier: CustomerTableViewCellId) as CustomerTableViewCell

        return cell

    }

效果是:

转载请注明:在路上 » 【已解决】swift中实现列表左滑出现编辑等其他按钮

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
55 queries in 0.088 seconds, using 18.33MB memory