2016-07-22 3 views
2

내 테이블보기에 검색 막대가 필요하지만 초보자이며 머리를 부러 뜨릴 수 있습니다.섹션이있는 TableView에서 검색 막대를 만드는 방법은 무엇입니까?

내 테이블보기에는 두 개의 섹션이 있습니다. 각 섹션에는 이름과 날짜가 포함 된 항목이 채워집니다. 사용자가 이름을 통해 조사 할 수 있기를 바랍니다.

한 가지 더 : 더 쉽고 체계적으로 만들기 위해 열거 형을 만들었고이 이름을 사용하여 섹션 이름을 지정하고 싶습니다.

class Cinema: UITableViewController { 

// MARK: - Properties 

var movie:[(type: Genre, data:[Movies])] = [] 

//MARK: - Super Methods 
override func viewDidLoad() { 
    super.viewDidLoad() 

    movie = [ 

     (Genre.Action, [ 

      Movies (name: "Matrix", year: "1999", description: "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers."), 

      Movies (name: "Gladiator", year: "2000", description: "When a Roman general is betrayed and his family murdered by an emperor's corrupt son, he comes to Rome as a gladiator to seek revenge."), 

      Movies (name: "Saving Private Ryan", year: "1998", description: "Following the Normandy Landings, a group of U.S. soldiers go behind enemy lines to retrieve a paratrooper whose brothers have been killed in action.") 
      ]), 

     (Genre.Drama, [ 

      Movies (name: "Good Will Hunting", year: "1997", description: "Will Hunting, a janitor at M.I.T., has a gift for mathematics, but needs help from a psychologist to find direction in his life."), 

      Movies (name: "Schindler's List", year: "1993", description: "In Poland during World War II, Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.") 
      ]), 

     (Genre.Comedy, [ 

      Movies (name: "Monty Python and the Holy Grail", year: "1975", description: "King Arthur and his knights embark on a low-budget search for the Grail, encountering many, very silly obstacles."), 

      Movies (name: "Hangover", year: "2009", description: "Three buddies wake up from a bachelor party in Las Vegas, with no memory of the previous night and the bachelor missing. They make their way around the city in order to find their friend before his wedding.") 

      ]) 
    ] 


} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return movie.count 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return movie[section].data.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ADTableViewCell 

    cell.title.text = movie[indexPath.section].data[indexPath.row].name 
    cell.year.text = movie[indexPath.section].data[indexPath.row].year 

    return cell 
} 

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if section == 0 { 
     return "Action" 
    } else if section == 1 { 
     return "Drama" 
    } else { 
     return "Comedy" 
    } 
} 

}

가 PS :

내가 도달했습니다 얼마나 멀리 보여주는 예했습니다 사용자가 자신이 선택한 라인을 클릭하면, 그가가있는 UIViewController로 전송됩니다 그 이름, 연도 및 설명이 표시됩니다. 이 예제에서는 prepareForSegue를 작성하지 않았습니다.

미리 감사드립니다.

답변

0

난 당신이 내 프로젝트에서 그것을 사용하고 구현 및 유지 보수가 정말 쉬웠다 한이 Link

를 따라 reccomment 것이다.

모든 검색 텍스트가 없으면 검색 텍스트

2와 일치

1

이 초과 입력 테이블 섹션과 행을 새로 고침 : 두 배열 결과를 관리 할 것 텍스트.

구현에 문제가 있으면 알려주십시오.

0

내가 제안하는 것은 영화 및 필터링 된 영화 배열을 갖는 것입니다. 데이터 소스는 기본적으로 영화가 될 필터링 된 영화 여야합니다. 검색 필드가있는 필드를 검색 할 때 검색 필드의 텍스트에 따라 조건에 따라 영화 배열을 필터링해야합니다. 그런 다음 테이블 뷰의 데이터를 다시로드해야하며 검색 항목 만 나타나야합니다. 구현 방법에 따라 텍스트 필드를 변경할 때마다 또는 편집을 중지하거나 반환 할 때마다 수행 할 수 있습니다.

var filteredMovie:[(type: Genre, data:[Movies])] = [] 

func yourTextfieldDelegate(textField: Textfield) { 
    if textField.text.characters.count == 0{ 
     self.filteredMovie = self.movie 
    } 
    else{ 
     // for example, filter by name 
     self.filteredMovie = movie.filter($0.data.name.lowercase.contains(textfield.text.lowercase)) 
     self.tableViewRef.reloadData() 
    } 

} 

ps : tableView의 콘센트가 누락되었습니다. 나는 그것을 tableViewRef라고 불렀다.

관련 문제