2016-06-17 3 views
0

Swift 프로젝트와 관련하여 질문이 있습니다. TableViewController에 여러 개의 셀이 있고 그 중 하나를 클릭하면 데이터가 tableViewController에서 전달되는 ViewController으로 이동합니다.viewcontroller에서 테이블 뷰의 다음 셀로 이동

ViewController에있는 단추를 구현하면 ViewController을 테이블보기 컨트롤러의 "다음 셀"의 내용으로 표시 할 수 있습니다. 예를 들어, 테이블 뷰 컨트롤러의 두 번째 셀을 클릭 했으므로 해당 셀에 해당하는 데이터를 표시하는 ViewController으로 이동 한 다음 해당 "다음"버튼을 클릭하면 내용이 표시됩니다. viewController 테이블 뷰 컨트롤러의 세 번째 셀 어떻게하면 깨끗하게 처리 할 수 ​​있습니까? 여기

내가 tableViewController에서 ViewController에 데이터를 전달하는 데 사용하는 코드입니다 :

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if let a = articles { 
     return a.count 
    } 
    return 0 
} 

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

    let article = articles?[indexPath.row] 

    if let a = article { 
     cell.textLabel?.text = a.articleName 
     cell.textLabel?.font = UIFont(name: "Avenir", size: 18) 
     cell.textLabel?.numberOfLines = 0 
     if let i = a.tableViewImage { 
      cell.imageView?.image = UIImage(named: i) 
     } 

    } 

    return cell 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ShowArticle" { 
     let articleVC = segue.destinationViewController as? ArticleViewController 

     guard let cell = sender as? UITableViewCell, 
      let indexPath = tableView.indexPathForCell(cell) else { 
       return 
     } 

     articleVC?.article = articles?[indexPath.row] 
    } 

    } 

viewController에서 오른쪽 기사를 보여, 나는 viewDidLoad() (내 viewController이 쓴의 웹보기를 보여줍니다

if let a = article { 
     articleWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(a.articleLink, ofType: "html")!))) 
    } 

나는 nextButton에 연결된 한 : 기사, 나는 articleLink에서 오는 클래스 Article)가3210 :

@IBOutlet weak var nextButton: UIButton! 

나는 스위프트에 비교적 새로운 해요, 그리고 내가 tableViewController에 내 데이터를 선언하기 때문에 그것을하는 방법을 모르고는 (내 큰 문제가 없다, 그리고 나는에 머물 수 표시되지 않습니다 ViewControllertableViewController에서 "가져 오기"데이터).

답변

1
아래 또한 다음 버튼을 내가 당신을 모르는이

var articles: [article]! 
var selectedIndex: Int! 

//Now your button click 
@IBAction func nextButtonClick(sender: UIButton) { 
    if ((self.selectedIndex + 1) < self.articles.count) { 
     self.selectedIndex++ 
     let nextArticle = self.articles[self.selectedIndex] 
     articleWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(nextArticle.articleLink, ofType: "html")!))) 
    } 
} 

같은 변경을 클릭 지금처럼 ViewController에 두 개의 전역 변수를 추가하려면이

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ShowArticle" { 
     let articleVC = segue.destinationViewController as? ArticleViewController 
     guard let cell = sender as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) else { 
      return 
     } 
     articleVC?.articles = articles 
     articleVC?.selectedIndex = indexPath.row 
     articleVC?.article = articles?[indexPath.row] 
    } 
} 

처럼 TableViewController'sprepareForSegue의 코드를 변경

NSArray 또는 Array을 사용하고 있으므로 TableViewController에서 만든 것과 동일한 articles 배열 객체를 만듭니다.

희망이 도움이 될 것입니다.

+0

당신은 생명의 은인입니다, 감사합니다! 그것은 매력처럼 작동합니다. 방금 nextLink를 nextButtonClick에서 nextArticle.articleLink로 변경했습니다 (누군가 앞으로 이것을 필요로하는 경우). 도움을 주셔서 감사합니다. – EdouM

+0

나는 대답을 다시 받았다. – EdouM

관련 문제