有人可以帮助我在email
单元格中显示tableView
字段吗? amount
字段正在显示,但我无法同时显示email
。
struct Posts {
var amount:String
var email:String
}
class PaymentRequestVC: UIViewController, UITableViewDelegate {
let tableView = UITableView()
var posts = [Posts]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
self.tableView.dataSource = self
self.tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell2")
tableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 900)
setupViews()
setupTableView()
loadPosts()
self.tableView.reloadData()
}
func loadPosts() {
let dbUsers = Firestore.firestore().collection("Payouts")
dbUsers.addSnapshotListener { (querySnapshot, error) in
if let error = error {
print("\(error.localizedDescription)")
} else {
for document in (querySnapshot?.documents)! {
if let Amount = document.data()["amount"] as? String {
let Email = document.data()["email"] as? String
print(Amount)
var post = Posts(amount: "", email: "")
post.amount = Amount
post.email = Email ?? ""
self.posts.append(post)
}
}
self.tableView.reloadData()
print(self.posts)
}
}
}
private func setupViews() {
let stackView: UIStackView = {
let sv = UIStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.spacing = 28
sv.axis = .vertical
sv.distribution = .fill
sv.alignment = .fill
return sv
}()
view.addSubview(stackView)
view.addSubview(tableView)
}
func setupTableView() {
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor)
])
}
}
extension PaymentRequestVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < posts.count {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let post = posts[indexPath.row]
cell.textLabel?.text = post.amount
cell.textLabel?.textColor = UIColor.black
return cell
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
let post = posts[indexPath.row]
cell2.detailTextLabel?.text = post.email
cell2.detailTextLabel?.textColor = UIColor.black
return cell2
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
答案 0 :(得分:1)
快捷键5
您需要使用 UITableViewCell.CellStyle.subtitle 初始化单元格,以使 detailTextLabel 属性起作用。
在扩展程序中,更改 cell 初始化:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < posts.count {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
let post = posts[indexPath.row]
cell.textLabel?.text = post.amount
cell.textLabel?.textColor = UIColor.black
cell.detailTextLabel?.text = post.email
cell.detailTextLabel?.textColor = UIColor.black
return cell
} else {
let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
let post = posts[indexPath.row]
cell2.detailTextLabel?.text = post.email
cell2.detailTextLabel?.textColor = UIColor.black
return cell2
}
请注意,如果要同时显示 amount 和 mail ,则使用 else 将只显示金额或电子邮件,就像下面的代码一样
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < posts.count {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
let post = posts[indexPath.row]
cell.textLabel?.text = post.amount
cell.textLabel?.textColor = UIColor.black
return cell
} else {
let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
let post = posts[indexPath.row]
cell2.detailTextLabel?.text = post.email
cell2.detailTextLabel?.textColor = UIColor.black
return cell2
}
所以更好的方法是同时返回两者,而 else 不显示任何内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < posts.count {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
let post = posts[indexPath.row]
cell.textLabel?.text = post.amount
cell.textLabel?.textColor = UIColor.black
cell.detailTextLabel?.text = post.email
cell.detailTextLabel?.textColor = UIColor.black
return cell
} else {
let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
//Empty cell returned if the condition above is not fulfilled
return cell2
}
希望这会有所帮助。
答案 1 :(得分:0)
我认为您在cellForRowAt中的逻辑不正确。
您有一系列的帖子。您想在同一单元格中显示金额和电子邮件吗?一个标签是标题,另一个标签是详细文本。
两个标签都位于同一UITableViewCell中,因此每个帖子只需要设置一次即可。
此外,如果indexPath.row大于帖子数,则posts [indexPath.row]实际上会使您的代码崩溃。因此,else语句实际上应该只是记录错误或什么都不做。
尝试这样的事情:
import numpy as np
import pandas as pd
import timeit
from numba import jit
np.random.seed(1)
df = pd.DataFrame(np.random.randint(0,100,size=(1000000, 1)), columns=['TR'])
def sma(df):
# code copied from the question
samples = len(df)
# MAverage
tr = df['TR']
df['trsum'] = float(0)
trsum = df['trsum']
df['Avg Range'] = float(0)
ma = df['Avg Range']
trsum[1] = tr[1]
for ii in range(2, samples):
trsum[ii] = trsum[ii - 1] + tr[ii]
if ii > 19:
ma[ii] = trsum[ii] / 20
trsum[ii] = trsum[ii] * 19 / 20
return df
def sma_numpy(df):
tr = 0
trsum = 1
ma = 2
samples = len(df)
df['trsum'] = float(0)
df['Avg Range'] = float(0)
npa = df.to_numpy()
npa[1,1] = npa[1,0]
for ii in range(2, samples):
npa[ii,trsum] = npa[ii-1,trsum] + npa[ii,tr]
if ii > 19:
npa[ii,ma] = npa[ii,trsum] / 20
npa[ii, trsum] *= 19 / 20
return pd.DataFrame(data=npa, columns=df.columns)
@jit(nopython=True)
def sma_numba_loop(npa):
tr = 0
trsum = 1
ma = 2
samples = len(npa)
for ii in range(2, samples):
npa[ii, trsum] = npa[ii - 1, trsum] + npa[ii, tr]
if ii > 19:
npa[ii, ma] = npa[ii, trsum] / 20
npa[ii, trsum] *= 19 / 20
def sma_numba(df):
df['trsum'] = float(0)
df['Avg Range'] = float(0)
npa = df.to_numpy()
npa[1, 1] = npa[1, 0]
sma_numba_loop(npa)
return pd.DataFrame(data=npa, columns=df.columns)
df_small = df[0:100_000].copy()
print(sma_numba(df[0:30].copy())) # JIT compile to save time
print("def sma() Pandas: ", timeit.Timer(lambda: sma(df_small.copy())).timeit(number=1), f's for {len(df_small)} rows', sep='')
print("def sma_numpy() Numpy: ", timeit.Timer(lambda: sma_numpy(df.copy())).timeit(number=1), f's for {len(df)} rows', sep='')
print("def sma_numba() Numpy + Numba: ", timeit.Timer(lambda: sma_numba(df.copy())).timeit(number=1), f's for {len(df)} rows', sep='')
'''
Check a sample to make sure they all return the same values
print(sma(df_small.copy())[10000:10010])
print(sma_numpy(df.copy())[10000:10010])
print(sma_numba(df.copy())[10000:10010])
'''