2015-09-28 102 views
-1

的值,我想在另外一個獲取來自其他功能迅速

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ObsequeCell 
{ 
    var cell : ObsequeCell! = tableView.dequeueReusableCellWithIdentifier("cell") as ObsequeCell 
    if(cell == nil) { 
     cell = NSBundle.mainBundle().loadNibNamed("cell", owner: self, options: nil)[0] as ObsequeCell; 
    } 
    /// CODE //// 
    var containerTitle = posts.objectAtIndex(indexPath.row).valueForKey("title") as NSString as String 
    cell.title.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as NSString as String 
    cell.descriptionOb.text = htmlLessString 
    println(htmlLessString) 

    return cell as ObsequeCell 

} 
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 

    filtered = containerTitle.filter({ (text) -> Bool in 
     let tmp: NSString = text 
     let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) 
     return range.location != NSNotFound 
    }) 
    if(filtered.count == 0){ 
     searchActive = false; 
    } else { 
     searchActive = true; 
    } 
    self.tableView.reloadData() 
} 

使用值從函數所以我需要containerTitle的價值,如果有人有想法得益於使用它在我的搜索欄功能

回答

-1

您可以在頁面的頂部創建一個新的可選變量從兩個功能,這可訪問:

如:

var myVarIwantToUse:String = "" // New Variable 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ObsequeCell 
{ 
    var cell : ObsequeCell! = tableView.dequeueReusableCellWithIdentifier("cell") as ObsequeCell 
    if(cell == nil) { 
     cell = NSBundle.mainBundle().loadNibNamed("cell", owner: self, options: nil)[0] as ObsequeCell; 
    } 
    /// CODE //// 
    var containerTitle = posts.objectAtIndex(indexPath.row).valueForKey("title") as NSString as String 
self.myVarIwantToUse = containerTitle //Transferring it to variable at top of page 
     cell.title.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as NSString as String 
    cell.descriptionOb.text = htmlLessString 
    println(htmlLessString) 

    return cell as ObsequeCell 

} 
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
let myVarInDifferentFunction = myVarIwantToUse //Destination 
    filtered = containerTitle.filter({ (text) -> Bool in 
     let tmp: NSString = text 
     let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) 
     return range.location != NSNotFound 
    }) 
    if(filtered.count == 0){ 
     searchActive = false; 
    } else { 
     searchActive = true; 
    } 
    self.tableView.reloadData() 
} 
+0

你的變量是「可選的」? – Grimxn

+0

我嘗試過這種方法,但它看起來像var myVarIwantToUse仍然是空的(抱歉我的英語) – matheo972