2016-09-10 2 views
0

SO에서 비슷한 질문이있을 수 있지만 아무 것도 결과를 얻지 못해 결국 여기에서 묻습니다.'UITableViewCell'을 반환 할 것으로 예상되는 함수에서 Mssing이 반환됩니다.

시나리오

나는 내부의 tableview와 서브 뷰를 내 HomeController .IN의 ViewController (HomeViewController)가 있습니다. 이 하위 뷰는 부분적으로 볼 수 있으며 사용자는 테이블 뷰 데이터를 표시하기 위해 뷰를 아래로 이동할 수 있습니다. Tableview는 메시지, 사진, 메모 및 이벤트와 같은 알림을 표시합니다. 내 애플리케이션은 그룹 채팅 전용 앱입니다. 따라서이 메시지, 사진, 메모 및 이벤트는 그룹을 기준으로 표시됩니다. 그룹에는 25 개의 메시지, 8 개의 사진, 2 개의 메모 및 1 개의 이벤트 세부 정보 만 표시됩니다. 이리저리 메시지를 보여주는 나는 TableviewCell를 생성하고이

var notifications : NotificationsView! 
notifications.frame = CGRectMake(0, -self.view.frame.height + 175, self.view.frame.size.width, self.view.frame.size.height) 
     notifications._tableview.dataSource = self 
     notifications._tableview.delegate = self 
     notifications._tableview.registerNib(UINib(nibName: "TimelineCell", bundle: nil), forCellReuseIdentifier: "timelinecell") 
     notifications._tableview.registerNib(UINib(nibName: "PhotosCell", bundle: nil), forCellReuseIdentifier: "photos") 
     notifications._tableview.registerNib(UINib(nibName: "NotesCell", bundle: nil), forCellReuseIdentifier: "notes") 
     notifications._tableview.registerNib(UINib(nibName: "CalendarCell", bundle: nil), forCellReuseIdentifier: "calendar") 

처럼 HomeViewController 내부에있는 TableView에 등록 된 내 모델 클래스는 같은 방법으로하는 것이 될 것입니다있는 TableView 안에이

class TimelineModal : Object { 
    dynamic var groupID   = "" 
    dynamic var groupName  = "" 
    let messages  = List<TimelineGroupMessages>() 
    let photos   = List<TimelineGroupPhotos>() 
    let notes   = List<TimelineGroupNotes>() 
    let calendarDetails = List<TimelineGroupCalendar>() 

    override class func primaryKey() -> String? { return "groupID" } 

} 

class TimelineGroupMessages : Object { 

    dynamic var message   = "" 
    dynamic var userName  = "" 
    dynamic var messagetype  = "" 
} 

class TimelineGroupPhotos : Object { 

    dynamic var photo   = "" 
} 

class TimelineGroupNotes : Object { 

    dynamic var noteTitle  = "" 

} 

class TimelineGroupCalendar : Object { 

    dynamic var calendarEventDate = "" 
    dynamic var calendarEventTitle = "" 

} 

데이터처럼 보이는 한 각 그룹의 메시지 (25)는 처음에는 사진 (8)을 표시 한 다음 Notes (2)와 마지막으로 이벤트 (1)를 표시하고 다시 동일한 그룹의 데이터가 다른 그룹에 표시됩니다.

데이터를 표시하는 경우 영역의 데이터는 viewWillAppear()

private var numberofRowsforTimeline : Int = 0 
    private var realmdata : Results<TimelineModal>! 
    private var groupsCount : Int = 0 
    private var messagesCount : Int = 0 
    private var photosCount : Int = 0 
    private var notesCount : Int = 0 
    private var eventsCount : Int = 0 

private func LoadDatafromRealm() 
    { 
let realm = try! Realm() 
     let realmDbData = realm.objects(TimelineModal) 
     print("Data : \(realmDbData)") 
     groupsCount = realmDbData.count 
     let messages_Count : Int = Int(realm.objects(TimelineGroupMessages).count) 
     let photos_Count : Int = Int(realm.objects(TimelineGroupPhotos).count) 
     let notes_Count  : Int = Int(realm.objects(TimelineGroupNotes).count) 
     let events_Count : Int = Int(realm.objects(TimelineGroupCalendar).count) 
     self.realmdata = realmDbData 

     numberofRowsforTimeline += messages_Count + photos_Count + notes_Count + events_Count 

} 

에 페치 그리고 여기에 내가 오류가 내가 잘못

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

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

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

     var cell : UITableViewCell! 
    // let cellMessages : TimelineCell = tableView.dequeueReusableCellWithIdentifier("timelinecell", forIndexPath: indexPath) as! TimelineCell 
    //let cellPhotos  : PhotosCell = tableView.dequeueReusableCellWithIdentifier("photos", forIndexPath: indexPath)   as! PhotosCell 
    //let cellNotes  : NotesCell  = tableView.dequeueReusableCellWithIdentifier("notes", forIndexPath: indexPath)   as! NotesCell 
    //let cellCalendar : CalendarCell = tableView.dequeueReusableCellWithIdentifier("calendar", forIndexPath: indexPath)  as! CalendarCell 

     for __data in self.realmdata 
     { 
      if __data.messages.count > 0 
      { 
       let cellMessages : TimelineCell = tableView.dequeueReusableCellWithIdentifier("timelinecell", forIndexPath: indexPath) as! TimelineCell 

       for x in 0..<__data.messages.count - 1 
        { 
         cellMessages.topConstraintforgroupNameLabel.constant = heightConstraints.topConstraintForRowZero.rawValue 
         cellMessages.topConstraintfortabNameLabel.constant = heightConstraints.topConstraintForRowZero.rawValue 
         cellMessages.lblGroupID.text = __data.groupID 
         cellMessages.lblGroupID.hidden = true 
         cellMessages.lblGroupName.text = __data.groupName 
         cellMessages.lblmessage.text = __data.messages[x].message 
         return cellMessages 
        } 

      } 
      else if __data.photos.count > 0 
      { 
       let cellPhotos : PhotosCell = tableView.dequeueReusableCellWithIdentifier("photos", forIndexPath: indexPath) as! PhotosCell 

       for p in 0..<__data.photos.count - 1 
       { 
        cellPhotos.imgPhoto1_photos.image = UIImage(imageLiteral: __data.photos[p].photo) 
        return cellPhotos 
       } 
          } 
      else if __data.notes.count > 0 
      { 
       let cellNotes : NotesCell  = tableView.dequeueReusableCellWithIdentifier("notes", forIndexPath: indexPath) as! NotesCell 

       for n in 0..<__data.notes.count - 1 
       { 
        cellNotes.lblNoteName1.text = __data.notes[n].noteTitle 
        return cellNotes 
       } 
          } 
      else if __data.calendarDetails.count > 0 
      { 
       let cellCalendar : CalendarCell = tableView.dequeueReusableCellWithIdentifier("calendar", forIndexPath: indexPath)  as! CalendarCell 
       for c in 0..<__data.calendarDetails.count - 1 
       { 
        cellCalendar.lblEventName1.text = __data.calendarDetails[c].calendarEventTitle 
       return cellCalendar 
       } 

      } 
      else 
      { 
       return cell 

      } 
     } 
} //-> Error Mssing return in a function expected to return 'UITableViewCell' 

언급 얻는 코드는? 미안 해요 내 질문을 너무 광범위하게 만든 경우. 방금 내가 필요한 걸 이해하도록 노력했습니다. 그게 전부 야. 도움! 도움! 도움!

참고 : 언급 된 오류가 발생하지 않았지만 Tableview에 모든 셀에 대해 동일한 데이터가있는 여러 가지 방법으로 Cell을 반환하려고했습니다.

+1

:

예를 들어, 다음은 메시지, 사진, 메모 및 이벤트에 대한 각 섹션의 행 각 그룹에 대한 섹션과 테이블 발생한다 대리자 및 데이터 소스 메서드는 tableView를 작성하는 데 사용됩니다. 특히'cellForRowAtIndexPath'는 여러 번 호출됩니다. 한 번씩 각 행에 대해 표시됩니다. indexPath 매개 변수는 필요한 섹션/행을 알려줍니다. 데이터를 반복 할 필요가 없습니다. indexPath를 사용하여 배열의 올바른 요소를 선택하면됩니다. 루프는 처음에는'return' 문이 실행될 때부터 모든 함수가 종료되므로 루프는 다음 번 반복으로 진행되지 않습니다. – pbasdf

+0

@pbasdf 나는 부분적으로 너를 이해하고있다. 내 예제와 똑같은 코드를 보여줄 수 있니? 그건 내가 더 잘 할거야 ... – Joker

+0

@pbasdf 어떻게 다차원 배열에서 올바른 요소를 얻으려면 indexPath를 사용할 수 있습니까?나는 조금 혼란스러운 상태에있다. – Joker

답변

0

tableView 대리인 및 datasource 메서드를 사용하여 tableView를 작성하는 방법을 잘못 이해했다고 생각합니다. 특히 cellForRowAtIndexPath 메소드는 표시 할 각 행에 대해 한 번씩 여러 번 호출됩니다. indexPath 매개 변수는 필요한 섹션/행을 알려줍니다. 데이터를 반복 할 필요가 없습니다. indexPath을 사용하여 배열의 올바른 요소를 선택하면됩니다. 처음으로 return 문을 실행하면 전체 함수가 종료되고 루프는 다음 반복으로 진행되지 않으므로 루프는 아무런 효과가 없습니다. 난 당신이 어떻게있는 tableView를 오해했다고 생각

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return self.groupsCount // separate section for each Group 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // locate the correct Group for the given section 
    let __data = self.realmdata[section] 
    // the total number of rows for this section (ie Group) is: 
    // number of messages + number of photos + number of notes + number of events: 
    return __data.messages.count + __data.photos.count + __data.notes.count + __data.calendarDetails.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // locate the correct Group for the given section (using the indexPath) 
    let __data = self.realmdata[indexPath.section] 
    if (indexPath.row < __data.messages.count) { // first rows are the messages 
     // use the indexPath.row to get the required index for the messages: 
     let messageIndex = indexPath.row 
     let cellMessages : TimelineCell = tableView.dequeueReusableCellWithIdentifier("timelinecell", forIndexPath: indexPath) as! TimelineCell 
     cellMessages.topConstraintforgroupNameLabel.constant = heightConstraints.topConstraintForRowZero.rawValue 
     cellMessages.topConstraintfortabNameLabel.constant = heightConstraints.topConstraintForRowZero.rawValue 
     cellMessages.lblGroupID.text = __data.groupID 
     cellMessages.lblGroupID.hidden = true 
     cellMessages.lblGroupName.text = __data.groupName 
     // use the index to locate the correct message text: 
     cellMessages.lblmessage.text = __data.messages[messageIndex].message 
     return cellMessages 
    } else if (indexPath.row < __data.messages.count + __data.photos.count) { // next are the photos 
     // use the indexPath.row (adjusted to account for the messages rows) to get the required index for the photos: 
     let photoIndex = indexPath.row - __data.messages.count 
     let cellPhotos : PhotosCell = tableView.dequeueReusableCellWithIdentifier("photos", forIndexPath: indexPath) as! PhotosCell 
     cellPhotos.imgPhoto1_photos.image = UIImage(imageLiteral: __data.photos[photoIndex].photo) 
     return cellPhotos 
    } else if (indexPath.row < __data.messages.count + __data.photos.count + __data.notes.count) { // next are the notes 
     let noteIndex = indexPath.row - __data.messages.count - __data.photos.count 
     let cellNotes : NotesCell  = tableView.dequeueReusableCellWithIdentifier("notes", forIndexPath: indexPath) as! NotesCell 
     cellNotes.lblNoteName1.text = __data.notes[noteIndex].noteTitle 
     return cellNotes 
    } else { // next are the Events 
     let eventIndex = indexPath.row - __data.messages.count - __data.photos.count - __data.notes.count 
     let cellCalendar : CalendarCell = tableView.dequeueReusableCellWithIdentifier("calendar", forIndexPath: indexPath)  as! CalendarCell 
     cellCalendar.lblEventName1.text = __data.calendarDetails[eventIndex].calendarEventTitle 
     return cellCalendar 
    } 
} 
+0

나는 너에게 빚이있다. :) – Joker

+0

다 잘되고 있지만 물어볼 질문이있다. 이벤트에 데이터가없는 경우 마지막으로 다른 경우를 보면 작동합니까? – Joker

+0

@Joker 이벤트에 데이터가 없으면 총 행 수는 25 + 8 + 2 + 0 = 35가됩니다.이 값은'numberOfRowsInSection'에 의해 반환 된 값이기 때문에 테이블 뷰는 행 0-34를 대응하는 indexPath 행 값으로'cellForRowAtIndexPath'를 호출합니다. 그러나 행 35 ("events"행)를 채우려는 시도조차하지 않습니다. 그래서 마지막'else' 절이 실행되지 않습니다. – pbasdf

관련 문제