每次在自定义单元格

时间:2018-06-10 20:53:34

标签: ios swift uitableview

我有一个viewcontroller,它有一个tableview和一个底部的按钮。在每个单元格内都有一个单选按钮作为tapGesture。我想用选中的单元格数更新按钮。如果我的手势在自定义单元格中,而我的按钮在我的视图控制器中,我怎样才能使两者一起工作?

自定义单元格:

class SearchTalentCell: UITableViewCell {
@IBOutlet weak var userProfileImage: UIImageView!
@IBOutlet weak var talentUserName: UILabel!
@IBOutlet weak var selectedImg: UIImageView!
@IBOutlet weak var inviteSentImg: UIImageView!
var prospectRef: FIRDatabaseReference!
var currentTalent: UserType!

func setTalent(talent: UserType) {
    currentTalent = talent
    currentTalent.userKey = talent.userKey
}

override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self, action: #selector(selectTapped))
    tap.numberOfTapsRequired = 1
    selectedImg.addGestureRecognizer(tap)
    selectedImg.isUserInteractionEnabled = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


func configureCell(user: UserType, img: UIImage? = nil) {
    prospectRef = Cast.REF_PRE_PRODUCTION_CASTING_POSITION.child(ProjectDetailVC.currentProject).child(FIRDataCast.prospect.rawValue).child(CastingDetailVC.positionName).child(user.userKey)
    setTalent(talent: user)
    self.talentUserName.text = "\(user.firstName) \(user.lastName)"

    prospectRef.observeSingleEvent(of: .value, with: { (snapshot) in
        if let _ = snapshot.value as? NSNull {
            self.inviteSentImg.isHidden = true
        } else {
            self.inviteSentImg.image = UIImage(named: "inviteSent")
            self.inviteSentImg.isHidden = false
        }
    })
    if UserType.selectedTalentForSearch.contains(currentTalent.userKey) {
        selectedImg.image = UIImage(named: "radioSelected")

    } else {
        selectedImg.image = UIImage(named: "radioUnselected")

    }

    //Image Caching
    if img != nil {
        self.userProfileImage.image = img
    } else {

        if let imageURL = user.profileImage {

            let ref = FIRStorage.storage().reference(forURL: imageURL)
            ref.data(withMaxSize: 2 * 1024 * 1024, completion: { (data, error) in
                if error != nil {
                    print("ZACK: Unable to download image from Firebase Storage")
                } else {
                    print("ZACK: Image downloaded from Firebase Storage")
                    if let imgData = data {
                        if let img = UIImage(data: imgData) {
                            self.userProfileImage.image = img
                            SearchTalentVC.userProfileImageCache.setObject(img, forKey: imageURL as NSString)
                        }
                    }
                }
            })
        }
    }
}

@objc func selectTapped(sender: UITapGestureRecognizer) {

    if UserType.selectedTalentForSearch.contains(currentTalent.userKey) {
        selectedImg.image = UIImage(named: "radioUnselected")
        UserType.selectedTalentForSearch = UserType.selectedTalentForSearch.filter{$0 != currentTalent.userKey}
        print("Keys: \(UserType.selectedTalentForSearch)")

    } else {
        selectedImg.image = UIImage(named: "radioSelected")
        UserType.selectedTalentForSearch.append(currentTalent.userKey)
        print("Keys: \(UserType.selectedTalentForSearch)")

    }
}
}

的ViewController:

class SearchTalentVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var sendInviteButton: UIButton!
var searchingRole = [Cast]()
var unfilteredTalent = [UserType]()
var filteredTalent = [UserType]()
var selectedTalent = [UserType]()
var matchingTalentUserKeys = [String]()
var isFiltered = false
var selectedCounter = [String]()
var prospectRef: FIRDatabaseReference!
static var userProfileImageCache: NSCache<NSString, UIImage> = NSCache()
let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Talent"
    searchController.searchBar.barStyle = .black
    navigationItem.searchController = searchController
    definesPresentationContext = true
    searchController.searchBar.scopeButtonTitles = ["All", "Role Specific"]
    searchController.searchBar.tintColor = UIColor.white
    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self
    self.sendInviteButton.setTitle("Send Invite to \(UserType.selectedTalentForSearch.count) Prospects", for: .normal)
    getTalentProfiles()

}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不确定为什么你在单元格内使用单元格选择,而不是tableview委托函数func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

如果您使用didSelectRowAt,您可以拥有一系列选定的行,并且可以将selectedRows.count包含在按钮文本中。

希望有所帮助!