tableView
에 대한 사용자 지정 셀을 만들고 있습니다. 나는 신속한 파일을 만든 :옵션이있는 사용자 지정 UITableViewCell
import Foundation
import UIKit
class CellForPost: UITableViewCell {
@IBOutlet weak var postLikes: UILabel!
@IBOutlet weak var postText: UILabel!
@IBOutlet weak var postDate: UILabel!
@IBOutlet weak var postPhoto: UIImageView!
}
을 위임 방법에 구현 : 게시물 전체 내용이있는 경우
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! CellForPost
cell.postPhoto.image = UIImage.init(data: (posts[indexPath.item].postPhoto)! as NSData)
cell.postText.text = posts[indexPath.item].postText
cell.postLikes.text = String(posts[indexPath.item].postLikes!)
cell.postDate.text = timestampToDate(posts[indexPath.item].postDate!)
return cell
}
모든 것이 잘 작동하지만, 예를 들어 post
에 선택적에는 사진이 (가없는 경우 구조체)는 메시지 나는이 메시지를 이해
fatal error: unexpectedly found nil while unwrapping an Optional value
와 충돌, 그래서 나는
를 만들려고@IBOutlet weak var postPhoto: UIImageView?
은 옵션 값과 비슷하지만 작동하지 않습니다. 'cos 컴파일러는 unwrap 값을 셀에 삽입하기 전에 필요로합니다. 오후 8시 30 분 P.S. 그것이 0 일 때 줄 높이를 맞추기 위해 imageView
을 삭제하는 것에 대한 간단한 조언을 줄 수 있다면.
'func tableView (tableView : cellForRowAtIndexPath indexPath :)'는 델리게이트가 아닌 데이터 소스 메소드입니다. – Andrej