【发布时间】:2017-03-29 22:05:55
【问题描述】:
我是 IOS 开发新手,正在使用 swift 3.0 。我有 2 个具有相同布局的自定义 TableView。唯一的区别是通过 Json URL 返回的数据。一个 TableView 称为 HomePageC ,另一个称为 UserProfileC 。如何使用我在 Prototype HomePageC 中的内容并在 UserProfileC 中重用它?这就像拥有一个数据主页然后看到单个用户数据因此 userProfile 但布局相同,因为我认为执行 2 个相同的 TableView 是多余的。
这是HomePageC代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! HomePageTVC
cell.post.text = Posts[indexPath.row]
cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)
cell.fullname.tag = indexPath.row
cell.fullname.addTarget(self, action: #selector(HomePageC.Fullname_Click(sender:)), for: .touchUpInside)
cell.comments.setTitle(Comments[indexPath.row],for: UIControlState.normal)
cell.comments.tag = indexPath.row
cell.votes.setTitle(Votes[indexPath.row],for: UIControlState.normal)
cell.votes.tag = indexPath.row
cell.time.text = Times[indexPath.row]
cell.shares.setTitle(Shares[indexPath.row],for: UIControlState.normal)
cell.shares.tag = indexPath.row
cell.location.text = Locations[indexPath.row]
return cell
}
UserProfileC 代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! HomePageTVC
cell.post.text = Posts[indexPath.row]
cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)
cell.fullname.tag = indexPath.row
cell.fullname.addTarget(self, action: #selector(HomePageC.Fullname_Click(sender:)), for: .touchUpInside)
cell.comments.setTitle(Comments[indexPath.row],for: UIControlState.normal)
cell.comments.tag = indexPath.row
cell.votes.setTitle(Votes[indexPath.row],for: UIControlState.normal)
cell.votes.tag = indexPath.row
cell.time.text = Times[indexPath.row]
cell.shares.setTitle(Shares[indexPath.row],for: UIControlState.normal)
cell.shares.tag = indexPath.row
cell.location.text = Locations[indexPath.row]
cell.vote_status.text = Votes[indexPath.row]
return cell
}
我试图将它投射到主页,但它给出了一个错误,任何帮助都会很棒,因为我很迷茫。
【问题讨论】:
标签: ios swift uitableview swift3