Ios Swift类函数是否可以/应该返回完整的UIVIew
使用Swift类函数返回完整的UIView或UITableVIew是一种好的做法吗 我还没有看到任何这样做的例子,但我认为这是可能的 我希望它的行为像一个Javascript/角度控制器 期望的:Ios Swift类函数是否可以/应该返回完整的UIVIew,ios,swift,Ios,Swift,使用Swift类函数返回完整的UIView或UITableVIew是一种好的做法吗 我还没有看到任何这样做的例子,但我认为这是可能的 我希望它的行为像一个Javascript/角度控制器 期望的: @IBOutlet var tableView: UITableView! @IBAction func ReloadContent(sender: UIButton, type: Type) { if(type == "test"){ self.tableView = BuildView(
@IBOutlet var tableView: UITableView!
@IBAction func ReloadContent(sender: UIButton, type: Type) {
if(type == "test"){
self.tableView = BuildView().ReloadContent("test")
} else {
self.tableView = BuildView().ReloadContent("default")
}
}
当前不太可重用:
var sections = [[String:String]]()
var sectionRows = [[[String:String]]]()
@IBOutlet var tableView: UITableView!
@IBAction func ReloadContent(sender: UIButton) {
let path = "testcontent"
let parameters = [
"todo":"Token should come from somewhere.",
"todo2":"There may be other parameters"
]
// This loads the data into the table.
populateTableData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.frame = CGRectMake(0, 75, 320, 450);
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sectionRows[section].count
}
// This is what builds the cell for each row, and returns it.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
// Collect the data for each
let object = sectionRows[indexPath.section][indexPath.row] // as NSDictionary
cell.textLabel?.text = object["thingName"]
return cell
}
// Select management
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
println("Selected at \(indexPath.row)")
}
// Sets the height of the section header. Required to make the first section header appear.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
// Sets the height of the section header. Required to make the first section header appear.
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 25
}
// This creates a view for every row in the header.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var view = UIView()
var listName = UILabel()
var userName = UILabel()
// Populate the data for the cell.
listName.text = self.sections[section]["listname"]
userName.text = self.sections[section]["userName"]
listName.setTranslatesAutoresizingMaskIntoConstraints(false)
userName.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["listName": listName, "view": view, "userName": userName ]
view.addSubview(listName)
view.addSubview(userName)
var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[listName]-10-|", options: .AlignAllCenterY, metrics: nil, views: views)
view.addConstraints(horizontallayoutContraints)
var verticalLayoutContraint = NSLayoutConstraint(item: listName, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
view.addConstraint(verticalLayoutContraint)
// Returns the completed view.
return view
}
你能描述一下你所说的“已完成”是什么意思吗?谢谢你对@IanMacDonald的后续报道。我的意思是,在viewDidLoad函数中,可以这样调用一个函数,而不是当前构建视图的七个tableView函数:myView=BuildViews.ViewMainBackground是HTML/JS,最近才出现。我一直在零零碎碎地学习斯威夫特。我不明白你在问什么。UITableViewDatasource和UITableViewDelegate方法是在UITableView中提供数据或与数据交互的唯一方法。当划分使维护更容易,代码更可读时,为什么要创建臃肿的“构造我的视图”方法?将数据源和委托作为独立对象的实例,而不是像这样使用ViewController。