UIPickerView + TableViewCell +代表和数据源

发布于 2024-10-07 13:02:43 字数 127 浏览 0 评论 0原文

我有表视图控制器和单独的类来处理我的表视图单元。在 tableview 单元格内我有 pickerview。 如何实现位于 tableCell 类中的 pickerview 的委托和数据源,但我的委托在 tableview 控制器中起作用?

I have Table view Controller and separate class which handles for me tableviewcell. Inside the tableview cell I have pickerview.
How to implement delegate and datasource for pickerview which is in tableCell class but my delegate functions in tableview controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-10-14 13:02:43

对于 Swift:

  1. 在自定义表视图类中为 UIPickerView 创建插座:

    类 MyTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {
        @IBOutlet var myPickerView:UIPickerView!
    }
    
  2. 在 ViewController 的“cellForRowAtIndexPath”中添加委托和数据源:

    class myViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource {
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {            
            让 cell = tableView.dequeueReusableCellWithIdentifier("myCell") 作为!我的表视图单元格
    
            cell.myPickerView.dataSource = self
            cell.myPickerView.delegate = self
    
            返回单元格
        }
    }
    

For Swift:

  1. Create outlet for UIPickerView in custom table view class:

    class MyTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {
        @IBOutlet var myPickerView: UIPickerView!
    }
    
  2. Add delegate and datasource in "cellForRowAtIndexPath" in ViewController:

    class myViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate, UITableViewDataSource {
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {            
            let cell = tableView.dequeueReusableCellWithIdentifier("myCell") as! MyTableViewCell
    
            cell.myPickerView.dataSource = self
            cell.myPickerView.delegate = self
    
            return cell
        }
    }
    
清风夜微凉 2024-10-14 13:02:43

您可以让 tableView 控制器在创建 tableview 单元格时设置一个属性,指示它是委托和数据源。

在您创建的 tableviewcell 类上,只需添加一个作为 tableview 控制器实例的属性。就像

@property (nonatomic, retain) MyTableViewController * pickerDelegate;

然后在你的 cellForRowAtIndexPath 中你可以将该属性设置为 self

cell.pickerDelegate = self;

你可能还需要设置一些额外的属性,比如标签来区分每个单元格。我认为 tableviewcell 上的另一个属性(如 NSIndexPath)也可以。

You could have your tableView controller set a property on the tableview cells as they are created indicating that it is the delegate and datasource.

On the tableviewcell class you created just add a property that is an instance of you tableview controller. Like

@property (nonatomic, retain) MyTableViewController * pickerDelegate;

Then in your cellForRowAtIndexPath you can set that property to self

cell.pickerDelegate = self;

You might need to also set some sort extra property like a tag to distinguish between each cell. I would think another property on the tableviewcell like an NSIndexPath would do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文