2016-08-04 6 views
1

25 tabsUITabBarController에 있고 각 탭에 다른 데이터로 tableview이 표시됩니다. 모든 탭에서 동일한 을 다른 데이터로 사용할 수 있습니까? 남은 하단에 4 개의 탭이 더 많이 표시됩니다.다른 UserInterface를 사용하고 싶습니다.

P. : 저는 신속하게 코딩하고 있습니다.

답변

2

물론 가능합니다. 보기 컨트롤러의 새 인스턴스를 인스턴스화하고 데이터 소스에 연결하고 위임 한 다음 제시하십시오.

25 탭 막대 컨트롤러의 탭은 미묘합니다. 사용자 인터페이스를 재고해야합니다.

0

가능합니다.

스위치를 사용하여 선택한 탭 모음 항목 인덱스를 검색 할 수 있으며 필요에 따라 데이터 소스를 지정할 수 있습니다.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     //suppose index is your tab Bar item index 
     switch index { 
     case 0: 
      return YOUR_DATA_ARRAY1.count 
     case 1: 
      return YOUR_DATA_ARRAY2.count 
      . 
      . 
      . 
     case 25: 
      return YOUR_DATA_ARRAY25.count 
     default: 
      break 
     } 
    } 

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

     let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath: indexPath) 

     //You can use same switch here to access objects from your data array and use it to assign values for cell 
     return cell; 
    } 

SIDE 주는 : 데이터가 매우 큰 것처럼 사실 당신의 접근 방식은 잘못된 것입니다. 이 방법을 사용할 수는 있지만 다시 생각해 볼 것을 적극 권장합니다.

0

예 할 수 있습니다. 내 제안은 tableView로 기본 ViewController를 만들고 탭에 연결된 각 ViewController를 확장하는 것입니다. 그런 다음 연결된 모든 뷰 컨트롤러는 필요에 따라 데이터 배열을 변경해야합니다.

그럼, 기본 ViewController 이름이 BaseViewController.swift입니다. 해당 컨트롤러에서 당신은 그런 프로토콜 목록에서 필요한 대의원 및 데이터 소스를 추가

class BaseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

지금 그 후의 tableview의 권리를 추가로 삼았

var dataSource = [String]() 

같은 -은 dataSource라는 이름의 배열을 가지고있다.

var myTableView: UITableView! 

이제 viewDidLoad 메서드에서 테이블을 추가하십시오.

override func viewDidLoad() { 
     super.viewDidLoad() 

     self.myTableView = UITableView.init(frame: self.view.bounds) 
     self.myTableView.delegate! = self 
     self.myTableView.dataSource! = self 
     self.view.addSubview(self.myTableView) 
    } 

이제 데이터 소스 메소드를 구현하십시오.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return self.dataSource.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
     let cell = tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) 
     cell.textLabel.text = self.dataSource[indexPath.row] //assuming you only have a string in your array 
     return cell; 
} 

이제이 클래스를 확장하면됩니다. 첫 번째 탭이 ViewControllerA.swift와 일치한다고 가정 해 보겠습니다.

그래서, 단지 this-

class ViewControllerA: BaseViewController 

처럼 확장 한 다음 viewDidLoad 방법에서는 dataSource 배열로 데이터 배열을 할당합니다.

그래서 첫 번째 컨트롤러에는 배열 이름 인 dataA가 표시됩니다.

그래서, 당신은, 당신의 viewDidLoad 방법에서이 작업을 수행

당신은 dataSource 이미지, 제목 또는 여러 개의 레이블 클래스 객체처럼 복잡한 데이터를 표시해야하는 경우
self.dataSource = dataA 

, 당신은 단지 cellForRowAtIndexPath의를 대체 할 수 있습니다 그 특별한 계급. 그렇게하면 다른 컨트롤러에 영향을 미칩니다.

희망이 도움이됩니다.

관련 문제