0

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

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:

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?

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
IdaEmilie
  • 85
  • 1
  • 9
  • You have a print button inside each cell ? – Kujey Oct 05 '15 at 09:35
  • Yes, a flag button. And i want in the `didDeselectItemAtIndexPath` to detect when the flag button is pressed. How to do this? – IdaEmilie Oct 05 '15 at 09:39
  • Why especially in the didDeselectItemAtIndexPath ? You could just subclass your cell, give it a uibutton property and set its behavior inside your collectionviewcustomcell.m. – Kujey Oct 05 '15 at 09:41
  • Like how? (In swift) – IdaEmilie Oct 05 '15 at 09:43
  • :D I've made a reponse a while ago, but it's in objective C : http://stackoverflow.com/questions/23801418/uicollectionview-adding-image-to-a-cell/23802296#23802296 . Can you understand it or should I explain ? – Kujey Oct 05 '15 at 09:45
  • @Kujey - Please explain, i have never used Obj-C :D – IdaEmilie Oct 05 '15 at 09:50
  • I'll make an answer for that, however i've kind of stopped working on Xcode for a while so there will be very few lines of codes – Kujey Oct 05 '15 at 09:51
  • Updated my answer, hope it helps – Kujey Oct 05 '15 at 11:46

2 Answers2

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.

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.

Fennelouski
  • 2,411
  • 1
  • 18
  • 26
  • I want to print out the text when the user press a specific button inside the cell. Each cell has a report button, and when they press, i want to do the println code. – IdaEmilie Oct 05 '15 at 08:26
0

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

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

Now in the header, give it a printButton property :

var printButton: UIButton!

Then in the CustomCollectionViewCell class implementation :

// 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:

func prepareForReuse()
{
    super.prepareForReuse();

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

Finally you implement your printText function

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

    println(cell.imageText.text)
}
Kujey
  • 1,122
  • 6
  • 17