2015-07-15 1 views
0

Я создаю приложение iOS с использованием быстрой и Xcode версии 6.3Как фильтровать содержимое UITableView, которое имеет пользовательскую ячейку с панелью поиска в Swift?

Я создал табличный вид, панель поиска в качестве выхода.

После этого я создал новый класс ProjectTableviewCell как подкласс ячейки UITableview.

В моей раскадровке я подключил несколько ярлыков к прототипу ячейки.

Я реализовал методы поиска. И его данные фильтрации в tableviewcell правильно. Это все работает нормально.

В случае, когда я просматриваю любой контент, отображается список содержимого.

После операции фильтрации он отображает только конкретную фильтрованную матрицу.

Проблема теперь, когда я сталкиваюсь, в содержимом ячейки таблицы не отображается.

Мой ожидаемый результат: я хочу показать фильтрованный массив с существующим содержимым ячейки в моем представлении таблицы.

class myProjects: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate { 


@IBOutlet weak var projectTable: UITableView! 
@IBOutlet weak var searchProject: UISearchBar! 
var project = [String]() 
var searchActive : Bool = false 

var filtered:[String] = [] 
//Search Bar Methods 
func searchBarTextDidBeginEditing(searchBar: UISearchBar) { 
    searchActive = true; 
} 

func searchBarTextDidEndEditing(searchBar: UISearchBar) { 
    searchActive = false; 
} 

func searchBarCancelButtonClicked(searchBar: UISearchBar) { 
    searchActive = false; 
} 

func searchBarSearchButtonClicked(searchBar: UISearchBar) { 
    searchActive = false; 
} 

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

    filtered = project.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.projectTable.reloadData() 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if(searchActive) { 
     return filtered.count 
     } 
     return project.count; 
    } 



    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let textCellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! projectTableViewCell 
    let row = indexPath.row 

    if(searchActive){ 
     cell.textLabel?.text = filtered[indexPath.row] 

    } else { 
    (cell.contentView.viewWithTag(1) as! UILabel).text = project[row] as String 
    } 
    (cell.contentView.viewWithTag(3) as! UILabel).text = projectStatus[row] as String 
    (cell.contentView.viewWithTag(7) as! UILabel).text = ProjectDescription[row] as String 
    (cell.contentView.viewWithTag(12) as! UILabel).text = category[row] as String 
    (cell.contentView.viewWithTag(13) as! UILabel).text = subcategory[row] as String 
    (cell.contentView.viewWithTag(14) as! UILabel).text = project_Tags[row] as String 
    (cell.contentView.viewWithTag(15) as! UILabel).text = skill_Needed[row] as String 
    (cell.contentView.viewWithTag(16) as! UILabel).text = budget[row] as String 
    (cell.contentView.viewWithTag(17) as! UILabel).text = timeFrames[row] as String 
    (cell.contentView.viewWithTag(18) as! UILabel).text = location[row] as String 

    return cell 
} 

Я Новичок в Свифт. Ваша помощь будет очень признательна.

+2

Рекомендуется создать класс проекта, а не количество полученных вами массивов. – iRiziya

+0

Yaa Я понял, что сейчас я перешел на класс. –

ответ

0

Изменен как показано на рисунке ниже.

struct Project { 
    var title = "" 
    var current_state = "" 
    var description = "" 
    var category_name = "" 
    var subcategory_name = "" 
    var tags = "" 
    var skills = "" 
    var budget = "" 
    var estimated_time = "" 
    var location = "" 

    init(title: String, current_state: String, description: String, category_name: String, subcategory_name: String, tags: String, skills: String, budget: String, estimated_time: String, location: String){ 
     self.title = title 
     self.current_state = current_state 
     self.description = description 
     self.category_name = category_name 
     self.subcategory_name = subcategory_name 
     self.tags = tags 
     self.skills = skills 
     self.budget = budget 
     self.estimated_time = estimated_time 
     self.location = location 
    } 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let textCellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! projectTableViewCell 
     let row = indexPath.row 

     if(searchActive){ 
      (cell.contentView.viewWithTag(1) as! UILabel).text = filtered[row].title as String 
     } 

     else { 

      (cell.contentView.viewWithTag(1) as! UILabel).text = projectsArray[row].title as String 
      (cell.contentView.viewWithTag(3) as! UILabel).text = projectsArray[row].current_state as String 
      (cell.contentView.viewWithTag(7) as! UILabel).text = projectsArray[row].description as String 
      (cell.contentView.viewWithTag(12) as! UILabel).text = projectsArray[row].category_name as String 
      (cell.contentView.viewWithTag(13) as! UILabel).text = projectsArray[row].subcategory_name as String 
      (cell.contentView.viewWithTag(14) as! UILabel).text = projectsArray[row].tags as String 
      (cell.contentView.viewWithTag(15) as! UILabel).text = projectsArray[row].skills as String 
      (cell.contentView.viewWithTag(16) as! UILabel).text = projectsArray[row].budget as String 
      (cell.contentView.viewWithTag(17) as! UILabel).text = projectsArray[row].estimated_time as String 
      (cell.contentView.viewWithTag(18) as! UILabel).text = projectsArray[row].location as String 
     } 
     return cell 
    } 
Смежные вопросы