2016-05-31 5 views
-2

나는 스크린 샷에서 언급 한 바와 같이 화면을 설계하는 요구 사항이 :swit을 사용하여 iOS에서 동적으로/프로그래밍 방식으로 여러 UITableView를 추가하는 방법은 무엇입니까?

enter image description here 다음 나는 데이터베이스에서 사용할 수있는 레코드 수에 따라 동적으로 UITableView 수 'N'을 추가 요청했다.

Google에서 많은 검색을했지만 자습서/가이드가 없습니다. 그래서이 질문은 저와 같은 모든 문제에 직면 해있는 많은 사람들에게 도움이 될 것이라고 생각합니다.

+0

"n tableviewrows"가 아닌 "n tableviews"가 맞습니까? –

+0

예 N 개의 테이블 뷰 및 N 개의 테이블 뷰가 없습니다. 내가 첨부 한 스크린 샷을 참조하십시오. – Saranya

+0

간단합니다. uiscrollview를 추가 한 다음 각 tableview의 태그로 원하는대로 uitableviews를 추가하십시오. – aBilal17

답변

1

가 정확히이 프로젝트 https://github.com/gabrieltheodoropoulos/iOS-Swift-PageControl처럼 실현할 수 있지만, 대신 jQuery과를 사용하여 UIView의를 사용하고 정확하게 당신이 원하는 일을 할 수 있습니다.

스토리 보드의 scrollView 프레임을 수정하여 기본 viewController를 사용자 정의하여 이름, 나이 및 업데이트 버튼을 추가 할 수 있습니다.

P. 프로젝트가 무거운에있는 경우 그것을 실현하기 위해 모든 코드를 수정하는 방법은 다음 예를 들어 아래의이 stack overflow post

더 나은 설명에 따라 메모리 소비에 대해, 당신은있는 tableView 변수를 다시 에 방법에 대해 생각 할 수 있습니다

class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate,UITableViewDataSource { 

    @IBOutlet weak var scrollView: UIScrollView! 
    @IBOutlet weak var pageControl: UIPageControl! 

    let totalPages = 8 // here you put your datasource array count 

    var currentTableView : UITableView! 

    let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor()] 

    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 

     configureScrollView() 
     configurePageControl() 
    } 

    func configureScrollView() { 

     scrollView.pagingEnabled = true 
     scrollView.showsHorizontalScrollIndicator = false 
     scrollView.showsVerticalScrollIndicator = false 
     scrollView.scrollsToTop = false 
     scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height) 
     scrollView.delegate = self 

     // Load the TestView view from the TestView.xib file and configure it properly. 
     for i in 0 ..< totalPages { 
      // Load the MyTable : a XIB contain a tableView 
      let myTable = NSBundle.mainBundle().loadNibNamed("MyTable", owner: self, options: nil)[0] as! UITableView 

      // Set its frame and the background color. 
      myTable.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height) 
      myTable.backgroundColor = sampleBGColors[i] 

      myTable.delegate = self 
      myTable.dataSource = self 
      self.currentTableView = myTable 

      let label = myTable.viewWithTag(1) as! UILabel 
      label.text = "Page #\(i + 1)" 

      scrollView.addSubview(myTable) 
     } 
    } 


    func configurePageControl() { 
     pageControl.numberOfPages = totalPages 
     pageControl.currentPage = 0 
    } 


    // MARK: UIScrollViewDelegate method implementation 

    func scrollViewDidScroll(scrollView: UIScrollView) { 
     // Calculate the new page index depending on the content offset. 
     let currentPage = floor(scrollView.contentOffset.x/UIScreen.mainScreen().bounds.size.width); 

     // Set the new page index to the page control. 
     pageControl.currentPage = Int(currentPage) 
    } 


    // MARK: IBAction method implementation 

    @IBAction func changePage(sender: AnyObject) { 
     // Calculate the frame that should scroll to based on the page control current page. 
     var newFrame = scrollView.frame 
     newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage) 
     scrollView.scrollRectToVisible(newFrame, animated: true) 

    } 

    // Add here UITableViewDelegate and UITableViewDatasource methods.. 
} 
+0

위대한 솔루션에 감사드립니다. 나는이 접근법을 시도 할 것이다. – Saranya

+0

나는 귀하의 접근 방식을 시도하고 그것은 대단합니다. 문제는 xib에 tableview를 추가하는 것입니다. 각 셀에는 확인란과 레이블이 있어야하기 때문입니다. 하지만 여기에 프로토 타입 셀이 없습니다. 이것을 달성 할 다른 방법이 있습니까? – Saranya

+0

물론, 다른 XIB를 UITableViewCell 형식으로 만들어야합니다. 이 stackoverflow 게시물에서 솔루션을 찾을 수 있습니다 : http://stackoverflow.com/a/25545185/1894067 –

0

일부 주석은 가로 스크롤과 함께 스크롤보기를 사용하고 동적으로 테이블 뷰를 추가 할 수 있지만 스크롤보기가 모든 테이블을 메모리에 유지하기 때문에 너무 많은 테이블 뷰가있을 경우 많은 메모리를 소비 할 수 있습니다. 페이징에도 똑같은 일이 발생할 수 있습니다.

표시 할 테이블 수가 너무 많으면 다시 사용할 수있는 무언가를 사용해야합니다.

그래서 저는 UICollectionView이 더 좋은 옵션이라고 생각합니다!

컬렉션보기에서 하나의 셀을 가져 와서 컬렉션보기의 셀에 tablview를 표시합니다.

당신은 Google에서 collectionview에서 tableview를 사용하는 방법에 대해 많은 도움을 얻을 수 있습니다. 이 도움이 될 것입니다

희망 : 당신은 몇 tableViews를 처리해야 투사하는 경우