代码
import UIKit
class ViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var suma=UITableViewCell()
var sampleviewpurple=UIView(frame: CGRect(x: suma.frame.width/0, y: 0, width: suma.frame.width/2, height: suma.frame.height))
sampleviewpurple.backgroundColor=#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
var sampleviewred=UIView(frame: CGRect(x: suma.frame.width/1, y: 0, width: suma.frame.width/2, height: suma.frame.height))
sampleviewred.backgroundColor=#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
suma.contentView.addSubview(sampleviewpurple)
suma.contentView.addSubview(sampleviewred)
return suma
}
@IBOutlet weak var tab: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tab.dataSource=self
tab.delegate=self
}
}
当我尝试将子视图添加到suma并返回到委托时,我的表视图仍为空白,当我将有效数据源返回到数据源时,并且还注意到对表视图的引用都很好,但是仍然无法将子视图添加到uitableviewcell
答案 0 :(得分:1)
在cellForRowAt
函数中,您必须使表格单元出队,而不是实例化UITableViewCell:
var suma = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")
答案 1 :(得分:0)
您正在用代码创建单元格。首先,您必须在import pandas as pd
import numpy as np
from datetime import timedelta
data = pd.read_csv('abc.csv')
data['date'] = pd.to_datetime(data['date'])
data.index = data['date']
pd.options.mode.chained_assignment = None
for a in data.index:
date_mask = (data.index >a) & (data.index < a+timedelta(days=60))
s=data['customer'][date_mask]
val=data.loc[a,'customer']
print(val in s.unique(),a)
方法中注册UITableViewCell
。
viewDidLoad
然后,您必须在cellForRowAt方法中使用出队单元格。
tab.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
如果要从var suma = tab.dequeueReusableCell(withIdentifier: "DefaultCell")
实例化cell
,则只需使用出队单元格。无需注册。
答案 2 :(得分:0)
要设置视图的框架,请使用尚未设置的单元格框架。您应该使用这样的视图,即已经设置了哪个框架,以及要基于哪个基础来计算新视图的框架。
尝试一下:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var sumaCell:UITableViewCell?
sumaCell = tableView.dequeueReusableCell(withIdentifier: "sumaCell")
if sumaCell == nil {
sumaCell = UITableViewCell(style: .default, reuseIdentifier: "sumaCell")
}
var sampleviewpurple=UIView(frame: CGRect(x: 0, y: 0, width: ((sumaCell?.frame.width)!/2), height: (sumaCell?.frame.height)!))
sampleviewpurple.backgroundColor=#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
var sampleviewred=UIView(frame: CGRect(x: (sumaCell?.frame.width)!/2, y: 0, width: ((sumaCell?.frame.width)!/2), height: (sumaCell?.frame.height)!))
sampleviewred.backgroundColor=#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
sumaCell?.contentView.addSubview(sampleviewpurple)
sumaCell?.contentView.addSubview(sampleviewred)
return sumaCell!
}
答案 3 :(得分:0)
我做了三个改变。
使单元出队以便可重复使用。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var suma = UITableViewCell()
suma = tab.dequeueReusableCell(withIdentifier: "DefaultCell")!`
更改了sampleviewpurple
和sampleviewred
的x位置
let sampleviewpurple = UIView(frame: CGRect(x: 0, y: 0, width: suma.frame.width/2, height: suma.frame.height))
let sampleviewred = UIView(frame: CGRect(x: suma.frame.width/2, y: 0, width: suma.frame.width/2, height: suma.frame.height))
在UITableView
函数中注册通过编程创建的单元格,以创建UITableViewCell
和viewDidLoad
之间的连接
tab.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")