我把tableview里面的数据都放在了另外一个controller,如何指定searchBar的outles到对应的controller
老师,我另外创建了controller继承了UITableViewDataSource和UITableViewDelegate,在里面操作tableView的操作,不在viewController里面操作,现在加入searchBar的时候由于它本身是放入viewcontroller的,所以好像找不到数据,报错了。。。
//
// TableViewController.swift
// Todo
//
// Created by qmore on 15-1-8.
// Copyright (c) 2015年 Qmore. All rights reserved.
//
import UIKit
func dateFromString(str:String) -> NSDate?{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.dateFromString(str) ;
return date
}
class TableViewController:NSObject,UITableViewDataSource,UITableViewDelegate{
weak var tableView: UITableView!
var todos : [TodoModel] = []
override init(){
todos = [TodoModel(id: "1", image: "selected-child", title: "去游乐场", date: dateFromString("2012-12-12")!),
TodoModel(id: "2", image: "selected-card", title: "去购物", date: dateFromString("2012-12-13")!),
TodoModel(id: "3", image: "selected-phone", title: "打电话", date: dateFromString("2012-12-14")!),
TodoModel(id: "4", image: "selected-travel",title: "去旅行", date: dateFromString("2012-12-15")!)]
}
init(tav : UITableView){
self.tableView = tav ;
}
func addTableView(ta : UITableView){
if(self.tableView == nil){
self.tableView = ta ;
}
}
/*获取内存数据库*/
func getTodos()->[TodoModel]{
return self.todos
}
/*添加数据*/
func appendTodos(todoItem:TodoModel)->Void {
self.todos.append(todoItem)
}
/*返回数据量*/
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
println("tableViewController 数据量\(self.todos.count)")
return self.todos.count ;
}
/*填充cell数据*/
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
println("tableViewController 填充数据")
let cell = self.tableView.dequeueReusableCellWithIdentifier("todoCell") as UITableViewCell
var todo = self.todos[indexPath.row] as TodoModel
var image = cell.viewWithTag(101) as UIImageView ;
var title = cell.viewWithTag(102) as UILabel;
var date = cell.viewWithTag(103) as UILabel;
/*填充cell::image*/
image.image = UIImage(named: todo.image)
/*填充cell::title*/
title.text = todo.title
/*格式化日期*/
let locale = NSLocale.currentLocale() ;
let dateFormat = NSDateFormatter.dateFormatFromTemplate("yyyy-MM-dd", options: 0, locale: locale)
let dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateFormat ;
/*填充cell::date*/
date.text = dateformatter.stringFromDate(todo.date)
return cell
}
/*删除操作*/
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.todos.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimation.Automatic ) ;
}
}
/*什么时候可以进行移动操作*/
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
println("can move ? \(self.tableView.editing)")
return self.tableView.editing ;
}
/*移动操作*/
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath){
println("move item")
/*要移动的数据*/
let todo = self.todos.removeAtIndex(sourceIndexPath.row) ;
/*放入移动后的位置*/
self.todos.insert(todo, atIndex: destinationIndexPath.row) ;
}
//
// ViewController.swift
// Todo
//
// Created by qmore on 15-1-7.
// Copyright (c) 2015年 Qmore. All rights reserved.
//
import UIKit
var tableViewController = TableViewController() ;
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var dataSource = tableViewController
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
println("viewController viewDidLoad")
super.viewDidLoad()
/*tableview source*/
dataSource.addTableView(self.tableView)
self.tableView.dataSource = dataSource ;
self.tableView.delegate = dataSource ;
/*inable editing button*/
navigationItem.leftBarButtonItem = editButtonItem()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.setEditing(editing, animated: animated)
}
/*确定按钮,链接回来*/
@IBAction func submitBack(segue:UIStoryboardSegue){
self.tableView.reloadData() ;
}
/*segue触发器,点击切换时候触发*/
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditTodo" {
var vc = segue.destinationViewController as DetailsViewController
var indexPath = tableView.indexPathForSelectedRow()
if let index = indexPath{
vc.todo = tableViewController.getTodos()[index.row]
}
}
}
}
}