简体   繁体   English

从选定的集合视图单元格获取信息

[英]Get info from selected collection view cell

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) . 这就像一个Instagram应用程序,因此有多个包含照片和照片文字的单元格,并且我有一个按钮,当按下按钮时,它要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. 但是它没有显示任何文字。

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. 如果您想在用户选择单元格时打印出文本。

Excuse my syntax errors, I've never done swift :D 请原谅我的语法错误,我从没做过swift: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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用CoreData从选定的集合视图单元格中获取标签文本? - How to Get Label Text from Selected Collection View Cell with CoreData? 显示从选择器视图到集合视图单元格的特定标签的选定值 - display the selected value from picker view to particularlabel of a cell of collection view 如何从位于集合视图单元格内的步进器获取选定的IndexPath? - How to get selected IndexPath from a stepper located inside a collection view cell? 从集合 vew 单元格中的选定单元格获取数据 - Get data from selected cell in collection vew cell 如何从iPhone的“表格”视图中获取选定单元格的单元格值 - how to get the cell value of a selected cell from Table view in iPhone Swift 4-如果已经选择,则取消选择集合视图单元格 - Swift 4 - deselect collection view cell if already selected 集合视图的reloadData方法显示选定的单元格 - Collection View reloadData method shows a cell as selected 集合视图在滚动时更改选定的单元格 - Collection View changes the selected cell on scrolling 从集合视图中的可重用单元格中获取按钮单击(XIB文件) - Get button click from reusable cell in collection view (xib file) Swift PHImageManager图片库图像选择了收集视图单元格后获取PHImageManagerMaximumSize - Swift PHImageManager Photo Gallery Image Get PHImageManagerMaximumSize after collection view cell selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM