首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在cellForRowAt indexPath外部访问UITableViewCell的行号

,可以通过以下方式实现:

  1. 在UITableViewDelegate的方法中,使用indexPath参数获取当前cell的行号。例如,在tableView(_:cellForRowAt:)方法中,可以将indexPath.row赋值给一个变量,然后在方法外部访问该变量即可。
代码语言:txt
复制
var selectedRow: Int?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    // 配置cell的内容
    selectedRow = indexPath.row
    return cell
}

// 在方法外部访问selectedRow变量
if let row = selectedRow {
    print("当前选中的行号是:\(row)")
} else {
    print("没有选中任何行")
}
  1. 可以通过给UITableViewCell添加一个tag属性来标识行号。在tableView(_:cellForRowAt:)方法中,将indexPath.row赋值给cell的tag属性,然后在方法外部通过viewWithTag方法获取到对应的cell,并读取其tag属性即可得到行号。
代码语言:txt
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    // 配置cell的内容
    cell.tag = indexPath.row
    return cell
}

// 在方法外部访问cell的行号
if let cell = tableView.viewWithTag(row) as? UITableViewCell {
    let row = cell.tag
    print("当前选中的行号是:\(row)")
} else {
    print("没有选中任何行")
}

以上是两种常见的方法,可以在UITableViewDelegate的相关方法中获取UITableViewCell的行号,并在方法外部进行访问。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • IOS UITableView UITableViewCell控件

    import UIKit class ViewController:UIViewController,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let screenRect = UIScreen.main.bounds let tableRect = CGRect(x:0, y:20, width: screenRect.size.width, height:screenRect.size.height - 20) let tableView = UITableView(frame:tableRect) tableView.dataSource = self self.view.addSubview(tableView) } func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int{ return 20 } func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell { let identifier = “reusedCell” var cell =tableView.dequeueReusableCell(withIdentifier:identifier) if(cell == nil) { cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:identifier) } cell?.textLabel?.text = “命运负责洗牌,玩牌的是我们自己!” return cell! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

    03

    IOS UIRefreshControl刷新控件

    import UIKit class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{ @IBOutlet weak var tabvLayout:UITableView! var refreshControl = UIRefreshControl() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.automaticallyAdjustsScrollViewInsets = false //添加刷新 refreshControl.addTarget(self, action:#selector(refreshData), for: UIControlEvents.valueChanged) refreshControl.attributedTitle =NSAttributedString(string:”松开后自动刷新”) tabvLayout.addSubview(refreshControl) refreshData() } // 刷新数据 func refreshData() { self.tabvLayout.reloadData() self.refreshControl.endRefreshing() } // MARK:- UITableViewDataSource func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int { return 10; } func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell { let cell = UITableViewCell(style:UITableViewCellStyle.value1, reuseIdentifier:“newsCell”) let date = NSDate() let timeFormatter = DateFormatter() timeFormatter.dateFormat = “yyy-MM-dd ‘at’ HH:mm:ss.SSS” //(时间格式) let strNowTime = timeFormatter.string(from:date as Date) as String cell.textLabel?.text = strNowTime let rect = CGRect(x:0,y:cell.frame.height-1,width:self.view.frame.size.width,height:1) let label = UILabel(frame:rect) label.backgroundColor = UIColor.lightGray() cell .addSubview(label) return cell; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

    03
    领券