Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Model Center   Code  
»áÔ±   
   
 
     
   
 ¶©ÔÄ
  ¾èÖú
ʹMVC ģʽÏ嵀 iOS Tableview
 
À´Ô´£ºblog.coding.net ·¢²¼ÓÚ£º 2017-5-23
  2068  次浏览      31
 

Èç¹ûÄãд¹ý iOS ÏîÄ¿µÄ»°£¬Ó¦¸Ã»áÁ˽⵽£¬iOS ÀïÃæ×î³£ÓõÄÒ»¸ö¿Ø¼þ¾ÍÊÇ UITableView£»¼´±ãûд¹ý iOS ÏîÄ¿£¬ÄãÓ¦¸ÃÒ²»áÔÚһЩÁ÷ÐÐµÄ App ÀïÃæ¿´µ½¹ýËü£¬±ÈÈ磺YouTube£¬Facebook£¬Twitter£¬Medium µÈµÈ¡£Ò»°ãÀ´½²£¬µ±ÄãÏëÒªÔÚÒ»¸öÒ³ÃæÉÏ£¬Õ¹Ê¾Ò»¸öÊýÁ¿¶¯Ì¬±ä»¯µÄÊý¾ÝµÄʱºò£¬ÄãÓ¦¸Ã»á¿¼ÂÇʹÓà UITableView¡£

»¹ÓÐÒ»¸ö»ù´¡¿Ø¼þÊÇ CollectionView£¬ËüÏà¶ÔÀ´½²¸üÁé»î£¬ËùÒÔÎÒ¸öÈ˸üϲ»¶ÓÃÕâ¸ö¡£ÉÔºóÎÒ»¹»áдһƪÎÄÕÂÀ´½²Ëü¡£

ËùÒÔ£¬ÔÚÄãµÄÏîÄ¿ÀïÃæ£¬²»¿É±ÜÃâµÄ»áÓõ½ UITableView¡£

±È½Ï³£¼ûµÄ×ö·¨ÊÇʹÓà UITableViewController£¬ËüÓÐÒ»¸öÄÚÖÃµÄ UITableView£»Í¨¹ý¼òµ¥µÄÉèÖþͿÉÒÔÈÃËü¹¤×÷ÆðÀ´£¬ÄãÐèÒª×öµÄÖ»ÊÇÉèÖúÃÊý×éÊý¾ÝºÍÏÔʾÊý¾ÝµÄ Cell¡£ËüʹÓÃÆðÀ´ºÜ¼òµ¥£¬¶øÇÒÒ²¿ÉÒÔÂú×ãÐèÇ󣬵«ÊÇËüÓÐÒ»¸öȱµã£ºÕâ»áÈà UITableViewController ÀïÃæµÄ´úÂë±äµÃ³¬¼¶³¤£¬¶øÇÒÕâ´òÆÆÁË MVC ģʽ¡£¹ØÓÚ MVC ¾ßÌåÊÇʲô£¬»òÕßÎÒÃÇΪʲôҪȥÁ˽âËü£¬Äã¿ÉÒÔÏÈ¿´Ò»Ï ÕâÆªÎÄÕ£¨ÒëÎÄ£©£¬ËüºÜºÃµÄ½éÉÜÁË iOS ÀïÃæËùÓеļܹ¹Ä£Ê½¡£

¼´±ãÄã²»ÏëȥŪ¶®ËùÓеÄÕâЩģʽ£¬ÖÁÉÙ¶ÔÓÚ UITableViewController ÀïÃæµÄÄÇÉÏǧÐдúÂ룬Äã×ÜÊÇÏëÒªÖØ¹¹»®·Öһϵİɡ£

ÔÚÎÒµÄÉÏһƪÎÄÕÂÀïÃæ£¬ÎÒÌáµ½ÁË ´Ó Controller Ïò Model ´«µÝÊý¾ÝµÄÈýÖÖ·½Ê½¡£

ÔÚÕâÆªÎÄÕÂÀïÃæ£¬ÎÒÒª½²µÄÊÇÎÒ´¦Àí tableView ËùÓеķ½Ê½£¬Ò²¾ÍÊÇÔÚÉÏÆªÎÄÕÂÀïÌáµ½µÄ - ´úÀíµÄ·½Ê½¡£ÓÃÕâÖÖ´¦Àí·½Ê½£¬¿ÉÒÔÈôúÂë¿´ÆðÀ´¸üÕû½à¡¢Ä£¿é»¯¡¢Ò×ÖØÓá£

Õâ´Î²»ÊÊÓà UITableViewController£¬¶øÊǰÑËü»®·Ö³É¼¸¸öÀࣺ

DRHTableViewController£ºUIViewController µÄ×ÓÀ࣬ȻºóÌí¼ÓÒ»¸ö UITableView ×÷Ϊ×ÓÊÓͼ

DRHTableViewCell£ºUITableViewCell µÄ×ÓÀà

DRHTableViewDataModel£ºËüÓÐÒ»¸ö API ·½·¨£º´´½¨Êý¾Ý²¢ÓôúÀíµÄ·½Ê½·µ»ØÊý¾Ý¸ø DRHTableViewController

DRHTableViewDataModelItem£ºÊý¾ÝÀࣺËü°üÀ¨ÁËչʾÔÚ DRHTableViewCell ÀïÃæµÄËùÓÐÊý¾Ý

ÏÈ´Ó UITableViewCell ¿ªÊ¼°É¡£

Ò»¡¢TableViewCell

ÒÔµ¥ÊÓͼӦÓã¨Single View Application£©ÎªÄ£°å£¬´´½¨Ò»¸öй¤³Ì£»È»ºóɾµô×Ô´øµÄ ViewController.swift ºÍ Main.storyboard Îļþ¡£ÉÔºóÎÒÃÇ»áÒ»²½²½µÄ´´½¨ËùÓÐÓõ½µÄÎļþ¡£

Ê×ÏÈ£¬´´½¨Ò»¸ö UITableViewCell µÄ×ÓÀà¡£Èç¹ûÄãÏëÓà XIB£¬¾Í¹´Ñ¡¡°Also create XIB file¡±Õâ¸öÑ¡Ïî¡£

ÔÚÕâÀÎÒÃÇÏëÒª×öµÄÊÇÒ»¸ö Medium Ö÷Ò³µÄ¼ò»¯°æ£¬ËùÒÔÐèÒªÌí¼ÓÏÂÃæÕâЩ×ÓÊÓͼ£º

Óû§Í·Ïñ

ÐÕÃû±êÇ©

ÈÕÆÚ±êÇ©

ÎÄÕ±êÌâ

ÎÄÕ¸ÅÒª

Ô¼ÊøÌõ¼þ£¨Autolayout£©Äã¿ÉÒÔËæÒâ¼Ó£¬Õâ²»ÊÇÖØµã¡£¸øÃ¿¸öÊÓͼÌí¼ÓÒ»¸ö¶ÔÓ¦µÄÊôÐÔ£¬ÍêÁËÔÚÄãµÄ DRHTableViewCell.swift ÎļþÀïÃæ£¬Ó¦¸ÃÓÐÀàËÆÏÂÃæµÄÕⲿ·Ö´úÂ룺

class DRHTableViewCell: UITableViewCell {
@IBOutlet weak var avatarImageView: UIImageView?
@IBOutlet weak var authorNameLabel: UILabel?
@IBOutlet weak var postDateLabel: UILabel?
@IBOutlet weak var titleLabel: UILabel?
@IBOutlet weak var previewLabel: UILabel?
}

ÔÚÕâÀÎÒ°Ñÿ¸ö @IBOutlet ĬÈ쵀 ¡°!¡± ¸Ä³ÉÁË ¡°?¡±¡£µ±Äã´Ó InterfaceBuilder ÀïÃæÍÏ×§ UILabel µ½´úÂëÀïµÄʱºò£¬Ëü»á×Ô¶¯Ç¿Öƽâ°ü¿ªÕâ¸ö±êÇ©£¬È»ºóÔÚËüºóÃæ¼ÓÉÏ ¡°!¡±¡£ÕâÀïÃæÓÐÒ»²¿·ÖÔ­ÒòÊÇΪÁËºÍ objective-C API ±£³ÖÒ»ÖÂÐÔ£¬µ«ÊÇÎÒ¸öÈË×ÜÊÇϲ»¶±ÜÃâÇ¿ÖÆ½â°ü£¬ËùÒÔÎÒÕâÀïÓà optional ±êʶ·û×öÁËÌæ»»¡£

½ÓÏÂÀ´£¬»¹ÐèÒªÒ»¸ö·½·¨£ºÓÃÊý¾ÝÈ¥Ìî³äÉÏÃæµÄÕâЩ±êÇ©ºÍͼƬ¡£ÔÚÊý¾ÝÕâ¿é£¬ÎÒÃDz»ÊÇÔÚ Cell Àï´´½¨ºÜ¶àµÄ±äÁ¿È¥±íʾËü£¬¶øÊÇΪËü´´½¨Ò»¸öеÄÀà DRHTableViewDataModelItem£º

class DRHTableViewDataModelItem {
var avatarImageURL: String?
var authorName: String?
var date: String?
var title: String?
var previewText: String?
}

×îºÃ»¹ÊÇÓà Date ÀàÐÍÈ¥´æ´¢ date£¬µ«ÊÇÕâÀïΪÁË·½±ã£¬¾Í°ÑËü´æ´¢³ÉÁË String ÐÍ¡£

ËùÓеıäÁ¿¶¼ÊÇ¿ÉÑ¡µÄ£¨optional£©£¬ËùÒÔ²»ÓÃÈ¥µ£ÐÄĬÈÏÖµµÄÎÊÌ⣬ÉÔºó»¹»áΪËüÌí¼ÓÒ»¸ö Init() ·½·¨¡£ÏÖÔÚÔٻص½ DRHTableViewCell.swift Îļþ£¬Ìí¼ÓÏÂÃæÕâЩ´úÂ루ÓÃÊý¾ÝÈ¥Ìî³ä Cell ÀïÃæµÄ±êÇ©ºÍͼƬ£©£º

func configureWithItem (item: DRHTableViewDataModelItem) {
// setImageWithURL(url: item.avatarImageURL)
authorNameLabel?.text = item.authorName
postDateLabel?.text = item.date
titleLabel?.text = item.title
previewLabel?.text = item.previewText
}

setImageWithURL() ·½·¨¾ßÌåµÄʵÏÖ£¬ÒÀÀµÓÚ¾ßÌåÏîÄ¿ÀïÃæ¶ÔͼƬ»º´æµÄ´¦Àí£»ËùÒÔÕâÀïûÓÐÈ¥¹ÜËü¡£

ÏÖÔÚÎÒÃÇÒѾ­ÓÐÁË Cell£¬¿ÉÒÔ´´½¨ TableView ÁË¡£

¶þ¡¢TableView

ÔÚÕâÀÎÒÃÇʹÓûùÓÚ¹ÊʰæµÄ£¨storyboard-based£©ViewController¡£Äã¿ÉÒÔÏÈ¿´Ï ÎÒµÄÉÏһƪÎÄÕ£¬Á˽âÏÂÔõô¸üºÃµÄʹÓùÊʰ档

Ê×ÏÈ£¬´´½¨Ò»¸ö UIViewController µÄ×ÓÀࣺ

ÔÚÕâÃæ£¬Óà UIViewController ¶ø²»ÊÇ UITableViewController£¬ÕâÑù¿ÉÒÔÓиü¶àµÄ¿ØÖÆ¡£±ÈÈç°Ñ UITableView ´´½¨³ÉÒ»¸ö×ÓÊÓͼ£¬¾Í¿ÉÒÔ¸ù¾Ý×Ô¼ºµÄÐèÒª£¬ÓÃÔ¼ÊøÌõ¼þÈ¥ÉèÖÃËüµÄλÖá£

½ÓÏÂÀ´£¬´´½¨Ò»¸ö¹ÊʰæÎļþ£¬ÓÃÏàͬµÄÃû×Ö¸øËüÃüÃû£ºDRHTableViewController¡£´Ó¶ÔÏó¿âÀïÃæÍÏ×§³öÀ´Ò»¸ö ViewController£¬²¢ÉèÖÃËüΪÉÏÃæ´´½¨µÄÀà¡£

Ìí¼ÓÒ»¸ö UITableView£¬²¢ÈÃËü¸ú View µÄËÄ±ß¶ÔÆë¡£

×îºó£¬ÔÚ DRHTableViewController ÀïÃæÌí¼Ó tableView ÊôÐÔ¡£

class DRHTableViewController: UIViewController {
@IBOutlet weak var tableView: UITableView?
}

ÎÒÃÇÒѾ­´´½¨ÁË DRHTableViewDataModelItem À࣬ÏÖÔÚÔÚ viewController ÀïÃæÌí¼ÓÒ»¸ö±¾µØ±äÁ¿

fileprivate var dataArray = [DRHTableViewDataModelItem]()

Õâ¸ö±äÁ¿ÓÃÀ´´æ´¢½«ÒªÕ¹Ê¾ÔÚ tableView ÉÏÃæµÄÊý¾Ý¡£

¼Çס£¬ÎÒÃDz»»áÔÚ ViewController ÀïÃæÈ¥´´½¨Êý¾Ý£ºdataArray Ö»ÊÇÒ»¸ö¿ÕÊý×飻¶øÊÇÔÚÉÔºóÓôúÀíµÄ·½Ê½¸øËüÌî³äÊý¾Ý¡£

ÏÖÔÚÔÚ viewDidLoad ·½·¨ÀïÃæÉèÖà tableView µÄһЩ»ù±¾ÊôÐÔ¡£ÔÚÕâÀïÑÕÉ«ºÍÑùʽ¶¼¿ÉÒÔËæÒâÉèÖ㬵«ÊÇΨһÐèҪȷÈϵÄÊÇ×¢²á nib Îļþ£º

tableView?.register(nib: UINib?, forCellReuseIdentifier: String)

ÔÚµ÷ÓÃÕâ¸ö·½·¨Ö®Ç°£¨Õâ¸ö·½·¨ÀïÃæµÄ identifier ²ÎÊýºÜÄÑд£©£¬ÎÒÃÇÏȲ»´´½¨ nib Îļþ£¬¶øÊÇÔÚ DRHTableViewCell ÀïÃæÌí¼ÓÁ½¸ö·½·¨£ºnib¡¢identifier¡£

Òª¾¡Á¿±ÜÃâÈ¥ÖØ¸´Ð´Ò»Ð©ºÜÄÑдµÄ×Ö·û´®£»Èç¹ûʵÔÚûÓа취£¬¿ÉÒÔ´´½¨Ò»¸ö ×Ö·û´®±äÁ¿£¬²¢ÓÃËüÀ´´úÌæ¡£

´ò¿ª DRHTableViewCell£¬ÔÚ¿ªÍ·Ìí¼ÓÏÂÃæµÄ´úÂ룺

class DRHMainTableViewCell: UITableViewCell {
class var identifier: String {
return String(describing: self)
}
class var nib: UINib {
return UINib(nibName: identifier, bundle: nil)
}
.....
}

±£´æÕâЩÐ޸ģ¬È»ºó»Øµ½ DRHTableViewController£¬µ÷Óà registerNib ·½·¨£º

tableView?.register(DRHTableViewCell.nib, forCellReuseIdentifier: DRHTableViewCell.identifier)

²»ÒªÍüÁËÉèÖà tableViewDataSource ºÍ tableViewDelegate Ϊ self£º

override func viewDidLoad() {
super.viewDidLoad()
tableView?.register (DRHTableViewCell.nib, forCellReuseIdentifier:
DRHTableViewCell.identifier)
tableView?.delegate = self
tableView?.dataSource = self
}

дÍêÖ®ºó£¬±àÒëÆ÷»á±¨´í£º¡°Cannot assign value of type DRHTableViewController to type UITableViewDelegate¡±

µ±ÄãʹÓà UITableViewController ×ÓÀàµÄʱºò£¬tableView µÄ´úÀíºÍÊý¾ÝÔ´ÊÇÒѾ­ÉèÖúÃÁ˵ġ£µ«ÊÇÈç¹ûÄãÊÇÔÚ UIViewController Öд´½¨ UITableView µÄ»°£¬¾ÍÐèÒªÈà UIViewController ¼Ì³ÐһϠUITableViewControllerDelegate ºÍ UITableViewControllerDataSource¡£

ֻҪΪ DRHTableViewController Ìí¼ÓÁ½¸öÀ©Õ¹£¬¾Í¿ÉÒÔ½â¾öÁË£º

extension DRHTableViewController: UITableViewDelegate {
}
extension DRHTableViewController: UITableViewDataSource {
}

Óֻᱨ´í£º¡°type DRHTableViewController does not conform to protocol UITableViewDataSource¡±¡£ÕâÊÇÒòΪÓÐһЩ±ØÐëʵÏֵķ½·¨£¬ÐèÒªÄãÔÚÕâ¸öÀ©Õ¹ÀïÃæÊµÏÖËüÃÇ£º

extension DRHTableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
}
}

UITableViewDelegate ËùÓеķ½·¨¶¼ÊǷDZØÐëµÄ£¬ËùÒÔ¼´Ê¹ÄãûÓÐʵÏÖ£¬ÕâÀïÒ²²»±¨´í¡£°´×¡ Command ¼ü£¬µã»÷ UITableViewDelegate£¬¿ÉÒÔ¿´µ½Ëü¾ßÌå¶¼ÓÐÄÄЩ·½·¨¡£Ëü×î³£Óõķ½·¨ÊÇ Ñ¡Ôñ/È¡ÏûÑ¡Ôñ ij¸ö cell£¬ÉèÖà cell ¸ß¶È£¬ÅäÖà tableView µÄ header/footer µÈ¡£

ÉÏÃæÁ½¸ö·½·¨¶¼ÊÇÐèÒª·µ»ØÖµµÄ£¬ËùÒÔ±àÒëÆ÷ÓÖ±¨´íÁË£º¡°Missing return type¡±¡£ÈÃÎÒÃÇÀ´½â¾öËü¡£

Ê×ÏÈ£¬ÐèÒªÉèÖà section ÀïÃæ row µÄÊýÁ¿£ºÎÒÃÇÒѾ­ÓÐÁË dataArray£¬¿ÉÒÔÖ±½ÓʹÓÃËüµÄ count ¾Í¿ÉÒÔ£º

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

ÔÚÕâÀÎÒûÓÐÖØÔØÁíÒ»¸ö·½·¨£ºnumberOfSectionsInTableView¡£Õâ¸ö·½·¨ÊǷDZØÐëµÄ£¬ËüĬÈÏÊÇ·µ»Ø 1£»¶øÕâ¸öÏîÄ¿ÀïÃæ tableView Ö»ÓÐÒ»¸ö section£¬ËùÒÔ²»ÐèÒªÈ¥ÖØÔØÕâ¸ö·½·¨¡£

×îºóÒ»²½£¬ÅäÖà UITableViewDataSource »¹ÐèÒªÔÚ cellForRowAtIndexPath ·½·¨ÀïÃæ·µ»Ø cell£º

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier:
DRHTableViewCell.identifier, for: indexPath) as? DRHTableViewCell
{
return cell
}
return UITableViewCell()
}

ÎÒÃÇ·ÖÐÐÀ´¿´Ò»Ï¡£

ΪÁË´´½¨ cell£¬ÎÒÃÇ¿ÉÒÔʹÓà DRHTableViewCell µÄ identifier ×÷Ϊ²ÎÊýÈ¥µ÷Óà dequeueReusableCell ·½·¨¡£Ëü»á·µ»ØÒ»¸ö UITableViewCell£¬ËùÒÔÎÒÃÇÐèÒªÓÃÒ»¸ö¿ÉÑ¡±êʶ·û°ÑËü´Ó UITableViewCell ת»»³É DRHTableViewCell£º

let cell = tableView.dequeueReusableCell(withIdentifier:
DRHTableViewCell.identifier, for: indexPath) as? DRHTableViewCell

È»ºó°²È«½â°üËü£¨safe-unwrap£©£ºÈç¹û³É¹¦£¬¾Í·µ»ØÕâ¸ö×Ô¶¨ÒåµÄ cell£º

if let cell = tableView.dequeueReusableCell(withIdentifier:
DRHTableViewCell.identifier, for: indexPath) as? DRHTableViewCell
{
return cell
}

Èç¹û°²È«½â°üʧ°Ü£¬¾Í·µ»ØÒ»¸öĬÈ쵀 UITableViewCell£º

if let cell = tableView.dequeueReusableCell (withIdentifier:
DRHTableViewCell.identifier, for: indexPath) as? DRHTableViewCell
{
return cell
}
return UITableViewCell()

ÎÒÃÇÊDz»ÊÇ©ÁËʲô£¿¶Ô£¬»¹ÐèÒªÓÃÊý¾ÝÈ¥ÅäÖà cell ÊÓͼ£º

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier:
DRHTableViewCell.identifier, for: indexPath) as? DRHTableViewCell
{
cell.configureWithItem(item: dataArray[indexPath.item])
return cell
}
return UITableViewCell()
}

ÎÒÃÇÒѾ­Îª×îºóÒ»²¿·Ö×öºÃ×¼±¸ÁË£º´´½¨ DataSource ²¢Á¬½Óµ½ TableView¡£

Èý¡¢DataModel

´´½¨Ò»¸ö DRHTableViewDataModel Àà¡£

ÎÒÃÇ»áÔÚÕâ¸öÀàÀïÃæ»ñÈ¡Êý¾Ý£¬ÖÁÓÚ»ñÈ¡·½Ê½£¬¿ÉÒÔÊÇ´ÓÒ»¸ö JSON Îļþ£¬»òÕßÊÇ HTTP ÇëÇ󣬻òÕßÊDZðµÄÊý¾ÝÔ´£¬Õâ²»ÊDZ¾ÎĵÄÖØµã¡£ÎÒÃǼٶ¨ÒѾ­ÓÐÁËÒ»¸ö API ·½·¨£¬Ëü¿ÉÒÔ·µ»ØÒ»¸ö¿ÉÑ¡ÀàÐ͵ÄÊý¾Ý¶ÔÏóºÍÒ»¸ö¿ÉÑ¡ÀàÐ͵ĴíÎóÐÅÏ¢£º

class DRHTableViewDataModel {
func requestData() {
// code to request data from API or local JSON file will go
here
// this two vars were returned from wherever:
// var response: [AnyObject]?
// var error: Error?
if let error = error {
// handle error
} else if let response = response {
// parse response to [DRHTableViewDataModelItem]
setDataWithResponse(response: response)
}
}
}

ÔÚ setDataWithResponse ·½·¨ÀïÃæ£¬ÎÒÃÇÐèÒªÓÃÒ»¸ö AnyObject ÀàÐ͵ÄÊý×é¶ÔÏó response£¬¹¹½¨³öÒ»¸ö DRHTableViewDataModelItem ÀàÐ͵ÄÊý×飻ËùÒÔ£¬½ô½Ó×ÅÌí¼ÓÏÂÃæÕâЩ´úÂ룺

private func setDataWithResponse(response: [AnyObject]) {
var data = [DRHTableViewDataModelItem]()
for item in response {
// create DRHTableViewDataModelItem out of AnyObject
}
}

ÔÚÕâ¸ö·½·¨ÀïÃæ£¬ÎÒÃÇ´´½¨ÁËÒ»¸ö DRHTableViewDataModelItem ÀàÐ͵ĿÕÊý×飬ÎÒÃÇÐèÒªÓà response Êý×éÈ¥¹¹½¨Ëü¡£È»ºóÎÒÃDZéÀú reponse Êý×éÀïÃæµÄÿ¸ö item£»ÔÚÕâ¸ö±éÀúÑ­»·ÀïÃæ£¬ÎÒÃÇÐèÒª¸ù¾Ý AnyObject ÀàÐ굀 item ´´½¨Ò»¸ö DRHTableViewDataModelItem ÀàÐ͵ĶÔÏó¡£

ÎÒÃÇ»¹Ã»Óиø DRHTableViewDataModel ´´½¨³õʼ»¯·½·¨£¬ËùÒԻص½ DRHTableViewDataModel À࣬´´½¨Õâ¸ö³õʼ»¯·½·¨¡£ÔÚÕâÀÎÒÃÇÓÃÒ»¸ö Dictionary [String: String]? ÀàÐ͵ĶÔÏó×÷Ϊ²ÎÊý£¬´´½¨Ò»¸ö Optional ÀàÐ͵ijõʼ»¯·½·¨£¨»òÕß˵ÊÇ ¿Éʧ°ÜµÄ³õʼ»¯£©¡£

init?(data: [String: String]?) {
if let data = data, let avatar = data[¡°avatarImageURL¡±], let name = data[¡°authorName¡±], let date = data[¡°date¡±], let title = data[¡°title¡±], let previewText = data[¡°previewText¡±] {
self.avatarImageURL = avatar
self.authorName = name
self.date = date
self.title = title
self.previewText = previewText
} else {
return nil
}
}

Èç¹ûÕâ¸ö Dictionary ÀïÃæ£¬È±ÉÙÁËÈÎÒâÒ»¸ö±ØÐèµÄ key Öµ£¬»òÕß˵Õâ¸ö×ֵ䱾Éí¾ÍÊÇÒ»¸ö nil µÄ»°£¬ÄÇôÕâ´Î³õʼ»¯¾ÍÊÇʧ°ÜµÄ£¨·µ»Ø nil£©¡£

ÓÐÁËÕâ¸ö¿Éʧ°ÜµÄ³õʼ»¯·½·¨£¨Failable Init£©£¬¾Í¿ÉÒÔ²¹È« DRHTableViewDataModel ÀàÀïÃæµÄ setDataWithResponse ·½·¨ÁË£º

private func setDataWithResponse(response: [AnyObject]) {
var data = [DRHTableViewDataModelItem]()
for item in response {
if let drhTableViewDataModelItem =
DRHTableViewDataModelItem(data: item as? [String: String]) {
data.append(drhTableViewDataModelItem)
}
}
}

ÔÚ for Ñ­»·Ö®ºó£¬ÎÒÃǵõ½ÁËÒ»¸ö DRHTableViewDataModelItem ÀàÐ͵ÄÊý×é¡£ÄÇôÎÒÃÇÔõô°ÑÕâ¸öÊý¾Ý´«µÝ¸ø TableView ÄØ£¿

ËÄ¡¢Delegate

Ê×ÏÈ£¬ÔÚ DRHTableViewDataModel.swift ÎļþÀïÃæ´´½¨Ò»¸ö´úÀí ЭÒé DRHTableViewDataModelDelegate£¬·ÅÔÚ DRHTableViewDataModel ÀàµÄÕýÉÏ·½£º

protocol DRHTableViewDataModelDelegate: class {
}

ÔÚÕâ¸öЭÒéÀïÃæ£¬´´½¨Á½¸ö·½·¨£º

protocol DRHTableViewDataModelDelegate: class {
func didRecieveDataUpdate (data: [DRHTableViewDataModelItem])
func didFailDataUpdateWithError(error: Error)
}

Swift ЭÒéÖУ¬class Õâ¸ö¹Ø¼ü×ÖÏÞ¶¨Á˸ÃЭÒéÖ»½ÓÊÜ class ÀàÐÍ£¨²»½ÓÊܽṹÌå»òÕßö¾ÙÀàÐÍ£©£¬´Ó¶ø¿ÉÒÔ¶ÔËüʹÓÃÈõÒýÓã¨weak reference £©¡£ÎªÁËÈ·±£´úÀíºÍίÍжÔÏóÖ®¼ä²»»áÓÐÑ­»·ÒýÓã¬ÔÚÕâÀïÐèÒªÓõ½ÈõÒýÓá£

È»ºó£¬ÔÚ DRHTableViewDataModel ÀïÃæÌí¼ÓÒ»¸ö¿ÉÑ¡µÄÈõÒýÓá£

weak var delegate: DRHTableViewDataModelDelegate?

ÏÖÔÚ£¬ÐèÒªÔÚ¿ÉÄÜÓõ½ËüµÄµØ·½µ÷ÓÃËü¡£¾ßÌåµ½Õâ¸öÀý×Ó£¬ÔÚÇëÇóʧ°ÜµÄʱºòÐèÒª´«µÝ´íÎóÐÅÏ¢£¬ÔÚ´´½¨³É¹¦µÄʱºòÐèÒª´«µÝÊý¾Ý¡£´íÎó´¦ÀíµÄ·½·¨¿ÉÒÔ·ÅÔÚ requestData ·½·¨ÀïÃæµ÷Óãº

class DRHTableViewDataModel {
func requestData() {
// code to request data from API or local JSON file will go
here
// this two vars were returned from wherever:
// var response: [AnyObject]?
// var error: Error?
if let error = error {
delegate?.didFailDataUpdateWithError(error: error)
} else if let response = response {
// parse response to [DRHTableViewDataModelItem]
setDataWithResponse(response: response)
}
}
}

×îºó£¬ÔÚ setDataWithResponse ·½·¨ÀïÃæµ÷Óõڶþ¸ö´úÀí·½·¨£º

private func setDataWithResponse(response: [AnyObject]) {
var data = [DRHTableViewDataModelItem]()
for item in response {
if let drhTableViewDataModelItem =
DRHTableViewDataModelItem(data: item as? [String: String]) {
data.append(drhTableViewDataModelItem)
}
}
delegate?.didRecieveDataUpdate(data: data)
}

Îå¡¢ÏÔʾÊý¾Ý

ÓÐÁË DRHTableViewDataModel ¾Í¿ÉÒÔÏò tableView ÀïÃæ´«µÝÊý¾ÝÁË¡£

Ê×ÏÈ£¬ÐèÒªÔÚ DRHTableViewController ÀïÃæ´´½¨ dataModel µÄÒýÓãº

private let dataSource = DRHTableViewDataModel()

È»ºó£¬»¹ÐèÒªÇëÇóÊý¾Ý¡£ÎÒ»áÔÚ ViewWillAppear ·½·¨ÀïÃæÈ¥×öÕâ¸öÊÂÇ飬ÕâÑùÿ´ÎÊÓͼ³öÏÖµÄʱºòÊý¾Ý¶¼»áµÃµ½¸üУº

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
dataSource.requestData()
}

ÕâÊÇÒ»¸ö¼òµ¥µÄÀý×Ó£¬ËùÒÔÎÒÔÚ viewWillAppear ·½·¨ÀïÃæÇëÇóÊý¾Ý¡£ÔÚÕæÕýµÄ app ÀïÃæ£¬ÕâÐèÒª¸ù¾ÝºÜ¶àÒòËØÊÓÇé¿ö¶ø¶¨£¬±ÈÈ绺´æÊ±¼ä¡¢API µÄʹÓá¢App ×ÔÉíµÄÂß¼­µÈµÈ¡£

È»ºó£¬ÔÚ viewDidLoad ·½·¨ÀïÃæ£¬°ÑËüµÄ´úÀí¸³Öµ¸ø self£º

dataSource.delegate = self

ÓÖ±¨±àÒë´íÎó£¬ÕâÊÇÒòΪ DRHTableViewController »¹Ã»Óм̳РDRHTableViewDataModelDelegate¡£ÔÚÎļþµÄĩβÌí¼ÓÏÂÃæµÄ´úÂë¾Í¿ÉÒԸ㶨£º

extension DRHTableViewController: DRHTableViewDataModelDelegate {
func didFailDataUpdateWithError (error: Error) {
}
func didRecieveDataUpdate(data: [DRHTableViewDataModelItem]) {
}
}

×îºó£¬ÎÒÃÇÐèÒª´¦Àí didFailDataUpdateWithError ºÍ didRecieveDataUpdate ÕâÁ½ÖÖÇé¿ö£º

extension DRHTableViewController: DRHTableViewDataModelDelegate {
func didFailDataUpdate WithError(error: Error) {
// handle error case appropriately (display alert, log an error, etc.)
}
func didRecieveDataUpdate(data: [DRHTableViewDataModelItem]) {
dataArray = data
}
}

¸ø dataArray ¸³Öµ¾Í±íʾ£¬ÆäʵÎÒÃÇÊÇÏëÒªÖØÐ¼ÓÔØ tableView µÄÊý¾ÝµÄ¡£µ«ÊÇÔÚÕâÀïÎÒÃDz¢Ã»ÓÐÔÚ didRecieveDataUpdate ·½·¨ÀïÈ¥×öÕâ¼þÊ£¬¶øÊÇÓÃ¶Ô dataArray Ìí¼Ó ÊôÐÔ¹Û²ìÕߣ¨property observer£©µÄ·½Ê½À´ÊµÏÖ£º

fileprivate var dataArray = [DRHTableViewDataModelItem]() {
didSet {
tableView?.reloadData()
}
}

ÉèÖÃÊôÐÔ¹Û²ìÕߣ¨Setter Property Observer£©»áÔÚÉèÖÃÍê³ÉÖ®ºó£¬ÔËÐÐËüÀïÃæµÄÕâЩ´úÂë¡£

¾ÍÊÇÕâЩ£¡

ÏÖÔÚ£¬ÄãÓÐÁËÒ»¸ö tableView Ä£°å£¬ËüÅäÖÃÁË×Ô¶¨ÒåµÄÊý¾ÝÔ´ºÍ×Ô¶¨ÒåµÄ cell¡£

Äã²»ÔÙÐèÒªÄǸö°ÑËùÓдúÂë¶¼¸ãÔÚÒ»Æð£¬ÅªÁËÓÐÉÏǧÐдúÂëµÄ tableViewController ÁË¡£

ÄãÉÏÃæ´´½¨µÄÿһ¸ö²¿·Ö£¬ÔÚÕû¸öÏîÄ¿Àï¶¼ÊÇ¿ÉÒÔÖØÓõ쬵±È»ÕâÊÇ×ö´úÂë»®·ÖµÄÁíÒ»¸öºÃ´¦ÁË¡£

   
2068 ´Îä¯ÀÀ       31
Ïà¹ØÎÄÕÂ

Éî¶È½âÎö£ºÇåÀíÀôúÂë
ÈçºÎ±àд³öÓµ±§±ä»¯µÄ´úÂë
ÖØ¹¹-ʹ´úÂë¸ü¼ò½àÓÅÃÀ
ÍŶÓÏîÄ¿¿ª·¢"±àÂë¹æ·¶"ϵÁÐÎÄÕÂ
Ïà¹ØÎĵµ

ÖØ¹¹-¸ÄÉÆ¼ÈÓдúÂëµÄÉè¼Æ
Èí¼þÖØ¹¹v2
´úÂëÕû½àÖ®µÀ
¸ßÖÊÁ¿±à³Ì¹æ·¶
Ïà¹Ø¿Î³Ì

»ùÓÚHTML5¿Í»§¶Ë¡¢Web¶ËµÄÓ¦Óÿª·¢
HTML 5+CSS ¿ª·¢
ǶÈëʽC¸ßÖÊÁ¿±à³Ì
C++¸ß¼¶±à³Ì