iOS开发Swift-UITableView-func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

发布时间 2023-09-18 15:28:10作者: 临易
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellid = "testCellID"    //cell的ID
        var cell = tableView.dequeueReusableCell(withIdentifier: cellid)  //对cell赋值
        if cell==nil {  //判断cell是否为空
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellid)
        }
        
        cell?.textLabel?.text = "这个是标题~"   //标题赋值
        cell?.detailTextLabel?.text = "这里是内容了油~"   //内容赋值
        cell?.imageView?.image = UIImage(named:"Expense_success")  //图标赋值
        return cell!  //返回cell
    }

 

 返回一个被强制解包的cell.

含义:对cell及cell中的内容进行配置.初始化和复用指定索引位置的UITableViewCell必须实现.

例如: