如何实现嵌套集合视图?

时间:2018-06-08 23:54:10

标签: ios swift uicollectionview uicollectionviewcell

我正在关注这个着名的tutorial 并希望在另一个collectionView中实现collectionView。我想我在ViewController.swift中几乎拥有它:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {
    let model: [[UIColor]] = generateRandomData()

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "outerCell", for: indexPath) as! OuterCollectionViewCell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView,
                        willDisplay cell: UICollectionViewCell,
                        forItemAt indexPath: IndexPath){
        guard let outerCollectionViewCell = cell as? OuterCollectionViewCell else { return }
        outerCollectionViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {

        return model[collectionView.tag].count
    }

    func collectionView(collectionView: UICollectionView,
                        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
                                                                         forIndexPath: indexPath)

        cell.backgroundColor = model[collectionView.tag][indexPath.item]

        return cell
    }
}

然后:

class OuterCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var innerCollectionView: UICollectionView!

    func setCollectionViewDataSourceDelegate
        <D: UICollectionViewDataSource & UICollectionViewDelegate>
        (dataSourceDelegate: D, forRow row: Int) {

        innerCollectionView.delegate = dataSourceDelegate
        innerCollectionView.dataSource = dataSourceDelegate
        innerCollectionView.tag = row
        innerCollectionView.reloadData()
    }
}

然而,Xcode很生气:ViewController与协议UICollectionViewDataSourceUICollectionViewDelegate的冗余一致性。这是可以理解的,因为我定义了两次..

如何在此处指定内部和外部collectionViews的委托方法之间的区别?

1 个答案:

答案 0 :(得分:0)

确实存在冗余,因为

1 -

var instanceOf = Factory.CreateViewModel();
instanceOf.AddingMethod(IEntity);

用这个

package schleusner.cameron;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    public static void main(String[] args) throws IOException, JAXBException         {

        Employees employees = new Employees();
        employees.setEmployees(new ArrayList<Employee>());

        Employee e1 = new Employee();
        e1.setName("codippa");
        e1.setLogin("4:30Pm");


        Employee e2 = new Employee();
        e2.setName("emp1");
        e2.setLogin("5:00PM");


        Employee e3 = new Employee();
        e3.setName("emp2");
        e3.setLogin("8:00AM");


        Employee e4 = new Employee();
        e4.setName("emp3");
        e4.setLogin("1:20AM");


        employees.getEmployees().add(e1);
        employees.getEmployees().add(e2);
        employees.getEmployees().add(e3);
        employees.getEmployees().add(e4);

        BufferedWriter writer1 = new BufferedWriter(new FileWriter("C:\\Users\\Cameron\\Desktop\\xmltest\\Employees.xml"));
        JAXBContext context = JAXBContext.newInstance(Employees.class);
        Marshaller m = context.createMarshaller();

        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        m.marshal(employees, writer1);
        writer1.close();
    }
}

VC应该只对这些协议符合一次

2 -

您只需要实现每个方法的一个副本并检查其中的collectionView的名称

package schleusner.cameron;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Employees")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employees {
  private List<Employee> employee;

  @XmlElement(name="Employee")
  public List<Employee> getEmployees() {
      return employee;
  }

  public void setEmployees(List<Employee> employeeList) {
      this.employee = employeeList;
  }
}