2015-09-28 7 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, чтобы использовать его в моей функции SearchBar, если кто-то есть идея спасибо

ответ

-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