iOS版的Radio Button和check box – AMChoice

AMChoice

iOS版的Radio Button和check box

ScreenShots

Usage

1-Add UIView to your view controller and set custom classAMChoice

2-Set the image for select and unselect (you can set it programmatically)

3-Creat new model and implementSelectableprotocol , by implementingSelectableprotocol you must add three variabel: (title,isSelected,isUserSelectEnable)

class VoteModel: NSObject,Selectable {
    var title: String
    var isSelected: Bool = false
    var isUserSelectEnable: Bool = true 
    
    init(title:String,isSelected:Bool,isUserSelectEnable:Bool) {
        self.title = title
        self.isSelected = isSelected
        self.isUserSelectEnable = isUserSelectEnable
    }
}

4-Set the data (items) for AMChoice view, the item may come from server or static in your code like i have done

amChoiceView.data = myItems

More

You can make any customise to the AMCHoice view (seeviewDidLoad) , or you can implement theAMChoiceDelegateprotocol to get the selected index, see the comment in the code below to know more

class ViewController: UIViewController,AMChoiceDelegate {
    
    
    @IBOutlet weak var amChoiceView: AMChoice!

    let myItems = [
        VoteModel(title: "Will smith", isSelected: false, isUserSelectEnable: true),
        VoteModel(title: "Al pacino", isSelected: false, isUserSelectEnable: true),
        VoteModel(title: "Abedalkareem", isSelected: false, isUserSelectEnable: true),
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        amChoiceView.isRightToLeft = false // use it to support right to left language
        
        amChoiceView.delegate = self // the delegate used to get the selected item when pressed
        
        amChoiceView.data = myItems // fill your item , the item may come from server or static in your code like i have done
        
        amChoiceView.selectionType = .single // selection type , single or multiple
        
        amChoiceView.cellHeight = 50 // to set cell hight
        
        amChoiceView.arrowImage = nil // use ot if you want to add arrow to the cell
        
        // you can set the selected and unselected image programmatically
        amChoiceView.selectedImage = UIImage(named: "selectedItem")
        amChoiceView.unselectedImage = UIImage(named: "unSelectedItem")
        
        
        
        
       
    }
    
    // get the selected item when pressed
    func didSelectRowAt(indexPath: IndexPath) {
        print("item at index \(indexPath.row) selected")
    }

   
    @IBAction func submit(_ sender: Any) {
        let selectedItems = amChoiceView.getSelectedItems() as! [VoteModel] // use getSelectedItems to get all selected item
        print(selectedItems)
        
        let selectedItemCommaSeparated = amChoiceView.getSelectedItemsJoined(separator: ",") // use getSelectedItemsJoined to get all selected item joined with separator (if the selection type multiple)
        print("\n\n\nComma Separated: \n \(selectedItemCommaSeparated)")
    }


}

Installation

Just addAMChoice.swiftin your project

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iOS版的Radio Button和check box – AMChoice