相关疑难解决方法(0)

如何从UITableViewCell获取UITableView?

我有一个UITableViewCell链接到一个对象,我需要告诉我单元格是否可见.从我已经完成的研究中,这意味着我需要以某种方式访问UITableView包含它的内容(从那里,有几种方法可以检查它是否可见).所以我想知道是否UITableViewCell有一个指针UITableView,或者是否还有其他方法从单元格获取指针?

iphone xcode objective-c tableview ios

97
推荐指数
8
解决办法
6万
查看次数

从Swift中的UITableViewCell子类中获取IndexPath

我有一个自定义UITableViewCell子类及其相关的xib.我在这个单元格中有一个UILabel和一个UIButton,我已经将按钮内部的触摸连接到子类.

我需要的是点击单元格中的按钮,以获取具有该按钮的单元格的索引路径.也许可以通过委托或其他东西将其发送回视图控制器.

因为我是里面UITableViewCell的子类,我不能使用的溶液等这个,因为我不从小区子类中,必须在tableview中的参考.经过进一步调查,我找到了另一个解决方案,我在Swift中实现了这个.

import UIKit

class ContactCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectionStyle = .None
    }

    @IBAction func callButtonPressed(sender: UIButton) {
        let indexPath = (self.superview as UITableView).indexPathForCell(self)
        println("indexPath?.row")
    }

}
Run Code Online (Sandbox Code Playgroud)

但是当我点击按钮时,它会崩溃,并显示一条错误消息,指出Swift动态转换失败.

知道我的代码有什么问题吗?

或者我愿意接受任何其他建议,这些建议可以让我以任何其他方式达到预期的结果.

谢谢.

uibutton uitableview nsindexpath ios swift

20
推荐指数
1
解决办法
2万
查看次数

如何使用按钮标签在swift中访问自定义单元格的内容?

我有一个应用程序,在自定义单元格中有自定义按钮.如果您选择单元格,它将转移到详细视图,这是完美的.如果我在单元格中选择一个按钮,下面的代码会将单元格索引打印到控制台中.

我需要访问所选单元格的内容(使用按钮)并将它们添加到数组或字典中.我是新手,因此很难找到如何访问单元格的内容.我尝试使用didselectrowatindexpath,但我不知道如何强制索引是标记的索引...

所以基本上,如果有3个单元格中有'Dog','Cat','Bird'作为每个单元格中的cell.repeatLabel.text,我选择第1行和第3行(索引0和2)中的按钮,它应该将'Dog'和'Bird'添加到数组/字典中.

    // MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return postsCollection.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    // Configure the cell...
    var currentRepeat = postsCollection[indexPath.row]
    cell.repeatLabel?.text = currentRepeat.product
    cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)

    cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton

    cell.checkButton.tag = indexPath.row;

    cell.checkButton.addTarget(self, …
Run Code Online (Sandbox Code Playgroud)

tags uibutton uitableview tableviewcell swift

19
推荐指数
3
解决办法
5万
查看次数

子类UITut中的UIButton需要调用父类的方法

如果答案已经存在,请道歉,但我找不到.

我有以下设置:MainViewController有一个很大的UITableView和CustomTableViewCell,它是UITableViewCell的子类.CustomTableViewCell的每个实例都在其内容视图中添加了一个UIButton(所有这些都以编程方式完成).

当在给定单元格中按下按钮时,我希望它在MainViewController中调用buttonPressed:方法,更好的是,告诉我包含按下按钮的单元格的indexPath.section.

CustomTableViewCell没有nib文件,都是以编程方式完成的.在CustomTableViewCell.h中,我声明:

    UIButton *mybutton;
Run Code Online (Sandbox Code Playgroud)

虽然我没有保留(没有@property,@synthesize).CustomTableViewCell.m的init方法如下所示:

    myButton = [[UIButton alloc] init];
    [myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventValueChanged];
    [[self contentView] addSubview:myButton];
    [myButton release];
Run Code Online (Sandbox Code Playgroud)

但我想调用生活在父视图中的"buttonPressed:"方法.一直在偷偷摸摸几个小时,所以如果有人能饶恕我自己的愚蠢,我将不胜感激.谢谢!

iphone uibutton uitableview

8
推荐指数
1
解决办法
4184
查看次数