不利用xib而是利用storyboard裏的默認動態cell來描述自定義cell
1、在第三個數據源方法中加載cellapp
{ MJAppCell *cell = [tableView dequeueReusableCellWithIdentifier:@"app"]; //將模型數據傳給cell的時候會調用set方法,在set方法中覆蓋按鈕的數據和狀態便可 cell.app = self.apps[indexPath.row]; return cell; }
PS:一個TableView能夠備份多種類型的cell,到時候只須要根據不一樣標識加載cell便可
2、在MJAppCell.h中添加模型數據屬性並重寫set方法atom
//這個MJApp是事先準備好直接拖過來的,裏面有app的icon,name,size,download屬性。 @property(nonatomic,strong)MJApp *app; //在set方法中將app的這些屬性賦值給cell的相應控件。 -(void)setApp:(MJApp *)app { _app = app; self.iconView.image = [UIImage imageNamed:app.icon]; self.nameView.text = app.name; self.introView.text = [NSString stringWithFormat:@" 大小: %@ | 下載量:% @",app.size,app.download]; }
3、點擊下載按鈕後顯示已下載,而且按鈕不能再被點擊
1.將Disabled時的文字設置爲已下載
2.在MJApp模型中添加一條屬性
@property(nonatomic,assign,getter = isDownloaded)BOOL downloaded;
3.監聽下載按鈕並實現方法spa
//點擊了下載按鈕 -(IBAction)downloadClick:(UIButton *)btn{ //讓按鈕失效 self.app.downloaded = YES; btn.enabled = NO; //2.通知代理 }
4.拿到下載按鈕,在set方法中覆蓋按鈕的狀態代理
-(void)setApp:(MJApp *)app { ...... // 覆蓋按鈕的狀態 self.downloadView.enabled = (self.app.isDownloaded == NO); }
5.添加一個下載成功的提示
1>定義協議並遵照協議code
@protocol MJAppCellDelegate <NSObject> @optional -(void)appCellDidClickedDownloadBtn:(MJAppCell *)cell; @end
@property (nonatomic,weak) id<MJAppCellDelegate> delegate;
2>在下載按鈕的方法中通知代理按鈕被點擊了orm
-(IBAction)downloadClick:(UIButton *)btn { .... //2.通知代理 if([self.delegate respondsToSelector:@selector (appCellDidClickedDownloadBtn:)]){ [self.delegate appCellDidClickedDownloadBtn:self]; } }
3>在第三個數據源方法中將控制器設置爲代理,並讓控制器遵照代理協議
4>實現cell 的代理方法
PS:何時用代理呢?
當一個View內部發生了一些事情,想告訴控制器的時候blog