Bagaimana cara menambahkan tombol hapus ke Sel Tampilan Koleksi di Swift?

Saat ini saya memiliki daftar nama pengguna yang dapat digulir menggunakan tombol Tampilan Koleksi. Tapi saya ingin menambahkan tombol hapus yang tumpang tindih ke setiap baris. Mereka harus dilampirkan ke tombol nama dan menggulirnya.

Bagaimana cara menambahkan tombol-tombol ini ke CollectionView saya? (Saya juga ingin melewatkan tombol hapus di baris pertama karena alasan yang jelas)

Layar Pemilihan Pengguna

Kode Saat Ini:

  //Add the cells to collection
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: UsernameCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UsernameCollectionViewCell
    cell.usernameLabel.text = userNames [indexPath.row]
    return cell
  }

  //Upon Selecting an item
  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if (indexPath.row == 0){
      self.performSegueWithIdentifier("newUserSegue", sender: self)
    }
    else {
      sendData(userNames[indexPath.row])
      self.dismissViewControllerAnimated(true, completion: nil)
    }

  }

person Justin Lewis    schedule 13.04.2015    source sumber


Jawaban (2)


Berhasil! Begini caranya:

  1. Saya menambahkan tombol ke sel di Storyboard.
  2. Menghubungkan outlet ke kelas UICollectionViewCell.
  3. Kode pengontrol tampilan telah diedit menjadi:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
      let cell: UsernameCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UsernameCollectionViewCell
    
      cell.usernameLabel.text = userNames [indexPath.row]
    
      cell.deleteButton?.layer.setValue(indexPath.row, forKey: "index")
      cell.deleteButton?.addTarget(self, action: "deleteUser:", forControlEvents: UIControlEvents.TouchUpInside)
    
      // Remove the button from the first cell
      if (indexPath.row == 0){
        var close : UIButton = cell.viewWithTag(11) as! UIButton
        close.hidden = true
      }
    
      return cell
    }
    
    func deleteUser(sender:UIButton) {
    
      let i : Int = (sender.layer.valueForKey("index")) as! Int
      userNames.removeAtIndex(i)
      UserSelectCollection.reloadData()
    }
    

Terima kasih banyak kepada JigarM atas contohnya di GitHub: https://github.com/JigarM/UICollectionView-Swift

person Justin Lewis    schedule 13.04.2015
comment
Apa itu UserSelectCollection? - person maidi; 28.11.2015
comment
UserSelectCollection kemungkinan besar adalah outlet tampilan koleksinya. - person FunkyMonk91; 27.02.2016
comment
Hai, saya telah menambahkan metode addTarget tetapi menunjukkan kesalahan Nilai tipe 'UILabel' tidak memiliki anggota 'addTarget' cara alternatif apa pun yang dapat dilakukan @Justin Lewis - person shahin ali agharia; 14.07.2016
comment
Halo teman-teman, Bisakah Anda menyarankan saya cara menambahkan pemrograman tombol hapus di tampilan koleksi? - person Saleem Khan; 17.01.2017

Mengapa tidak membuat UICollectionViewCell khusus di IB dan cukup menambahkan tombol ke dalamnya? Daftarkan ke collectionView Anda dengan:

- registerNib:forCellReuseIdentifier:

Anda dapat menggunakan delegasi atau notifikasi untuk memproses ketukan tombol.

person CryingHippo    schedule 13.04.2015