从所选集合视图单元格中获取信息

时间:2022-12-03 10:28:11

I am trying to print the cell.image.text from collection view cell.

我试图从集合视图单元格打印cell.image.text。

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

        let post = self.arrayOfDetails[indexPath.row]
        cell.currentUserLabel.text = post.username
        cell.imageText.text = post.text

        cell.uploadedTimeLabel.text = post.CreatedAt.timeAgo

        cell.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "Placeholder"))

        return cell
    }

ArrayOfDetails:

ArrayOfDetails:

var arrayOfDetails = [Details]()

Details:

细节:

struct Details {
    var username:String!
    var text:String!
    var CreatedAt:NSDate!
    var image:String!
    init(username:String,text:String,CreatedAt:NSDate,image:String){

        self.username = username
        self.text = text
        self.CreatedAt = CreatedAt
        self.image = image
    }
}

This is like a Instagram app, so there are multiple cells with photos and photo text, and i have a button, and when the button is pressed, it want to println(cell.image.text). How can i do this?

这就像一个Instagram应用程序,因此有多个单元格包含照片和照片文本,我有一个按钮,当按下按钮时,它想要println(cell.image.text)。我怎样才能做到这一点?

I tried:

我试过了:

@IBAction func printButton(sender: AnyObject) {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: nil) as! CollectionViewCell

        println(cell.imageText.text)
    }

But it did not show any text.

但它没有显示任何文字。

2 个解决方案

#1


0  

Instead of trying to get the cell what if you just got the data itself? Don't look at the collectionView(emphasis on View), instead pull up data from the Data Source.

如果你只获取数据本身,而不是尝试获取单元格?不要查看collectionView(强调View),而是从数据源中提取数据。

You could use

你可以用

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

and in that method call

并在那个方法调用

println(self.arrayOfDetails[indexPath.row].text)

if you wanted to print out the text when the user selects the cell.

如果您想在用户选择单元格时打印出文本。

#2


0  

Excuse my syntax errors, I've never done swift :D

请原谅我的语法错误,我从来没有做过快速:D

First of all you create a CustomCollectionViewCell, which subclasses CollectionViewCell.

首先,您创建一个CustomCollectionViewCell,它是CollectionViewCell的子类。

Now in the header, give it a printButton property :

现在在标题中,给它一个printButton属性:

var printButton: UIButton!

Then in the CustomCollectionViewCell class implementation :

然后在CustomCollectionViewCell类实现中:

// This is lazy loading, everytime something needs to talk to the printButton 
// of a cell, it checks if this button exists. If not, it will be created.
lazy var printButton: UIButton = 
{
    var temporaryButton: UIButton = UIButton()
    temporaryButton.frame = self.contentView.bounds;
    temporaryButton.addTarget(self action: @selector(printText), forControlEvents: TouchUpInside);
    self.contentView.addSubview(temporaryButton);
    return temporaryButton;
}()

    return _printButton;
}

Then you remove your printButton when your subclassed cell is reused:

然后在重用子类化单元格时删除printButton:

func prepareForReuse()
{
    super.prepareForReuse();

    self.printButton.removeFromSuperview().
    self.printButton = nil;
}

Finally you implement your printText function

最后,实现printText函数

func printText()
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: nil) as! CollectionViewCell

    println(cell.imageText.text)
}

#1


0  

Instead of trying to get the cell what if you just got the data itself? Don't look at the collectionView(emphasis on View), instead pull up data from the Data Source.

如果你只获取数据本身,而不是尝试获取单元格?不要查看collectionView(强调View),而是从数据源中提取数据。

You could use

你可以用

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

and in that method call

并在那个方法调用

println(self.arrayOfDetails[indexPath.row].text)

if you wanted to print out the text when the user selects the cell.

如果您想在用户选择单元格时打印出文本。

#2


0  

Excuse my syntax errors, I've never done swift :D

请原谅我的语法错误,我从来没有做过快速:D

First of all you create a CustomCollectionViewCell, which subclasses CollectionViewCell.

首先,您创建一个CustomCollectionViewCell,它是CollectionViewCell的子类。

Now in the header, give it a printButton property :

现在在标题中,给它一个printButton属性:

var printButton: UIButton!

Then in the CustomCollectionViewCell class implementation :

然后在CustomCollectionViewCell类实现中:

// This is lazy loading, everytime something needs to talk to the printButton 
// of a cell, it checks if this button exists. If not, it will be created.
lazy var printButton: UIButton = 
{
    var temporaryButton: UIButton = UIButton()
    temporaryButton.frame = self.contentView.bounds;
    temporaryButton.addTarget(self action: @selector(printText), forControlEvents: TouchUpInside);
    self.contentView.addSubview(temporaryButton);
    return temporaryButton;
}()

    return _printButton;
}

Then you remove your printButton when your subclassed cell is reused:

然后在重用子类化单元格时删除printButton:

func prepareForReuse()
{
    super.prepareForReuse();

    self.printButton.removeFromSuperview().
    self.printButton = nil;
}

Finally you implement your printText function

最后,实现printText函数

func printText()
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: nil) as! CollectionViewCell

    println(cell.imageText.text)
}