前兩天看了一些關於delegate的東西
在以前的時候,老師上課也講過,但是我們自己從來沒用過,應該是沒有想到過用它
就算老師講的demo裏面涉及的有,我就直接抄過來,也懶得考慮爲什麼要用它?怎麼用才合適
cell.h
#import <UIKit/UIKit.h>
@protocol MyCellDelegate;
@interface ZMCell :UITableViewCell
@property(weak,nonatomic)id <MyCellDelegate>delegate;
-(void)customlizedWithData:(id)data findAllname:(NSArray *)array;
@end
@protocol MyCellDelegate <NSObject>
-(void)TapSomeButtons:(UIButton *)sender;
@end
cell.m-(void)TapRowBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
if ([self.delegaterespondsToSelector:@selector(TapSomeButtons:)])
{
[self.delegateTapSomeButtons:btn];
}
}
-(ZMCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
staticNSString *CellIdentifier =@"cellID";
ZMCell * cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil)
{
cell = [[ZMCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
cell.delegate = self;
NSNumber * number = [[NSNumberalloc]init];
number = [NSNumbernumberWithInt:indexPath.row ];
[cell customlizedWithData:numberfindAllname:tableArr];
NSLog(@"%@",number);
return cell;
記住了,血的教訓-(void)TapSomeButtons:(UIButton *)sender
{
UIButton *btn = (UIButton *)sender;
// 根據tag獲取整條信息
ZMTable *table = [tableArrobjectAtIndex:btn.tag];
btn.selected = !btn.selected;
selectRoomAndTabelTag = [NSStringstringWithFormat:@"%d%d",table.roomid,btn.tag];
JDGSQLiteUtil *sqlite = [JDGSQLiteUtilsharedInstance];
[sqliteopenDatabase:@"A.DB"];
// 更新數據
int i= [selectRoomAndTabelTagintValue];
//點擊選中按鈕的時候
if (btn.selected)
{
[sqlite exec:[NSStringstringWithFormat:@"UPDate Table1 set isBusy = '1' WHERE Tableid = '%d'",i]];
}
// 再一次點擊沒有選中時
if (!btn.selected)
{
[sqlite exec:[NSStringstringWithFormat:@"UPDate Table1 set isBusy = '0' WHERE Tableid = '%d'",i]]; }
//查找當isBusy是1的時候
NSArray *selecttableID = [sqlitequery:@"SELECT Tableid FROM Table1 where isBusy = '1' "decode:bDecode];
NSLog(@"已選中的桌子%@",selecttableID);
//默認存在數據庫A.DB
BOOL isExisted = [sqlite existDB:@"A.DB"];
// 如果不存在
if (!isExisted )
{
UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"數據庫操作失敗" delegate:selfcancelButtonTitle:@"確定" otherButtonTitles:@"取消",nil];
[alertshow];
[btn setBackgroundImage:[UIImageimageNamed:@"free"]forState:UIControlStateNormal];
}
}