两个定向单元格内部集合视图,如何获取单元格标题?

时间:2016-08-04 07:42:56

标签: ios swift2 uicollectionview uicollectionviewcell

您好我想在UICollectionView内获取单元格标题,当我点击UICollectionView项目我希望得到单元格标题时,UICollectionView单元格内有1个单元格有类别数组。我的代码在这里。

查看控制器单元格

import UIKit

class ViewController: UIViewController {
    var categories = ["A", "B", "C", "D", "E"]
}

extension ViewController : UITableViewDelegate { }

extension ViewController : UITableViewDataSource {

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
         return categories[section]

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return categories.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow
       return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        print(indexPath.row)

    }

}

UICollectionView


import UIKit

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
}

extension CategoryRow : UICollectionViewDataSource {


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell
        return cell
    }

}

extension CategoryRow : UICollectionViewDelegateFlowLayout {


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let itemsPerRow:CGFloat = 4
        let hardCodedPadding:CGFloat = 5
        let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
        let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
        return CGSize(width: itemWidth, height: itemHeight)

    }


    //MARK :- UICollectionViewDelegate
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


    print(indexPath.row) // HERE gives collection view number when clicked BUT I WANT TO Get cell title which inside categories ARRAY example: A/3, B/5



    }




}

底部我标有

  

print(indexPath.row)// HERE在点击时给出了集合视图编号但我想获取单元格标题内部类别ARRAY示例:A / 3,B / 5

我希望在那里看到行动,谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

我认为你需要为你的班级添加标题权证

CategoryRow : UITableViewCell {
    var title: String?
    @IBOutlet weak var collectionView: UICollectionView!
}

您需要在CategoryRow

中添加方法
func prepareForReuse() {
    super.prepareForReuse()
    title = nil
 }
<{1>}中的

将此标题设置为单元格

ViewController

并在func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow let titleFromCategories = categories[indexPath.row] as? String cell.title = titleFromCategories return cell } 中实施如下

CategoryRow

答案 1 :(得分:1)

从阵列中获取标题如下:

QT       += core 
QT       += network
QT       -= gui

TARGET = QServer
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    tcpserver.cpp

HEADERS += \
    tcpserver.h

//===========================TCPServer.h


#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QTcpServer>

class TCPServer : public QObject
{
    Q_OBJECT
public:
    explicit TCPServer(QObject *parent = 0);
    ~TCPServer();
signals:

public slots:
    void newConnection();
    void disconnected();
    void readyRead();
    void writeData(QString data);

private:
    QTcpServer *serv;
    QTcpSocket *socket;
};
#endif // TCPSERVER_H

//===========================TCPServer.cpp


#include "tcpserver.h"

TCPServer::TCPServer(QObject *parent) : QObject(parent)
{
    serv=new QTcpServer(this);
    connect(serv,SIGNAL(newConnection()),this,SLOT(newConnection()));
    if(!serv->listen(QHostAddress::Any,3490))
    {
        qDebug()<<"Server Error in Connecting...."<<serv->errorString();
    }
    else
        qDebug()<<"Server Started     :)     ";
}

TCPServer::~TCPServer()
{
    disconnected();
}

void TCPServer::newConnection()
{
    socket=serv->nextPendingConnection();
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    socket->write("Hello Client");
    socket->flush();//clean the buffer
    socket->waitForReadyRead(3000);
   // sock->close();
}

void TCPServer::disconnected()
{
    socket->close();
    qDebug()<<"Good By     :)     ";
}

void TCPServer::readyRead()
{
    //qDebug()<<"Server is Reading ...."<<socket->readAll();
    while(socket->canReadLine())
    {
        QByteArray ba = socket->readLine();
        if(strcmp(ba.constData(), "!exit\n") == 0)
        {
            socket->disconnectFromHost();
            break;
        }
        //printf(">> %s", ba.constData());
        qDebug() << "Server is receiving ...."<<ba.constData();
    }
}


void TCPServer::writeData(QString data)
{
    socket->write(data.toStdString().c_str());
    socket->flush();//clean the buffer
    socket->waitForReadyRead(3000);
}


//===========================TCPServer/main.cpp


#include <QCoreApplication>
#include "tcpserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    TCPServer srv;
//    while(1)
//    {
        QTextStream s(stdin);
        QString value = s.readLine();
        srv.writeData(value);
//    }