จะเพิ่มปุ่มลบใน Collection View Cell ใน Swift ได้อย่างไร

ตอนนี้ฉันมีรายการชื่อผู้ใช้แบบเลื่อนโดยใช้มุมมองคอลเลกชันของปุ่ม แต่ฉันต้องการเพิ่มปุ่มลบที่ทับซ้อนกันในแต่ละแถว พวกเขาจะต้องแนบไปกับปุ่มชื่อและเลื่อนไปด้วย

ฉันจะเพิ่มปุ่มเหล่านี้ใน CollectionView ของฉันได้อย่างไร (และฉันต้องการข้ามปุ่มลบในแถวแรกด้วยเหตุผลที่ชัดเจน)

หน้าจอการเลือกผู้ใช้

รหัสปัจจุบัน:

  //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 แหล่งที่มา


คำตอบ (2)


ได้ผลแล้ว! มีวิธีดังนี้:

  1. ฉันเพิ่มปุ่มลงในเซลล์ในกระดานเรื่องราว
  2. เชื่อมต่อเต้าเสียบกับคลาส UICollectionViewCell
  3. แก้ไขโค้ดตัวควบคุมมุมมองเป็น:

    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()
    }
    

ขอขอบคุณ JigarM สำหรับตัวอย่างของเขาใน GitHub: https://github.com/JigarM/UICollectionView-Swift

person Justin Lewis    schedule 13.04.2015
comment
UserSelectCollectionคืออะไร? - person maidi; 28.11.2015
comment
UserSelectCollection น่าจะเป็นช่องทางการดูคอลเลกชันของเขา - person FunkyMonk91; 27.02.2016
comment
เฮ้ ฉันได้เพิ่มวิธี addTarget แล้ว แต่แสดงค่าข้อผิดพลาดประเภท 'UILabel' ไม่มีสมาชิก 'addTarget' ทางเลือกอื่นในการทำ @Justin Lewis - person shahin ali agharia; 14.07.2016
comment
สวัสดีเพื่อน ๆ คุณช่วยแนะนำวิธีเพิ่มการเขียนโปรแกรมปุ่มลบในมุมมองคอลเลกชันได้ไหม? - person Saleem Khan; 17.01.2017

ทำไมไม่สร้าง UICollectionViewCell แบบกำหนดเองใน IB แล้วเพิ่มปุ่มเข้าไป ลงทะเบียนไปที่คอลเลกชันของคุณดูด้วย:

- registerNib:forCellReuseIdentifier:

คุณสามารถใช้ผู้รับมอบสิทธิ์หรือการแจ้งเตือนเพื่อประมวลผลการแตะปุ่ม

person CryingHippo    schedule 13.04.2015