【问题标题】:Get info from selected collection view cell从选定的集合视图单元中获取信息
【发布时间】:2015-10-05 00:45:10
【问题描述】:

我正在尝试从集合视图单元格中打印 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:

var arrayOfDetails = [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
    }
}

这就像一个Instagram应用程序,所以有多个带有照片和照片文字的单元格,我有一个按钮,当按下按钮时,它想要println(cell.image.text)。我该怎么做?

我试过了:

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

        println(cell.imageText.text)
    }

但它没有显示任何文字。

【问题讨论】:

  • 每个单元格内都有一个打印按钮?
  • 是的,一个标志按钮。我想在didDeselectItemAtIndexPath 中检测何时按下标志按钮。如何做到这一点?
  • 为什么特别是在 didDeselectItemAtIndexPath 中?您可以将您的单元格子类化,给它一个 uibutton 属性并在您的 collectionviewcustomcell.m 中设置它的行为。
  • 怎么样? (迅速)
  • :D 我刚才已经做出了回应,但它在目标 C 中:stackoverflow.com/questions/23801418/…。你能理解还是我应该解释一下?

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

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

你可以使用

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

在那个方法调用中

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

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

【讨论】:

  • 我想在用户按下单元格内的特定按钮时打印出文本。每个单元格都有一个报告按钮,当他们按下时,我想做 println 代码。
【解决方案2】:

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

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

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

var printButton: UIButton!

然后在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;
}

然后,当您的子类单元格被重用时,您删除您的 printButton:

func prepareForReuse()
{
    super.prepareForReuse();

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

最后你实现了你的 printText 函数

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

    println(cell.imageText.text)
}

【讨论】:

  • 对不起,我不明白这一点。第一行代码放在哪里?
  • 这只是为您的 CustomCollectionViewCell 声明一个新属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 2016-08-19
  • 1970-01-01
相关资源
最近更新 更多