自定义Cell上的按钮--delegate协议的实现
2013-12-06 16:25
239 查看
前两天看了一些关于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];
}
}
设置一个delegate,让Controller去做Cell上按钮的事情
当delegate响应你在协议里面定义的方法的时候,就把按钮传过去(传到ViewController里面)
在这里面
-(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;
记住了,血的教训
我把cell.delegate = self;写在了 if (cell
== nil)之前
delegate是空的,我找了两个多小时,确定自己的delegate没有写错,也没有出现任何问题
高手过来一看,就说你得delagate放在 if (cell
== nil)之前,它可能是空的,所以delegate也可能是空的
把它换个位置就不会出现这样的问题了
响应delegate方法
-(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];
}
}
在以前的时候,老师上课也讲过,但是我们自己从来没用过,应该是没有想到过用它
就算老师讲的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];
}
}
设置一个delegate,让Controller去做Cell上按钮的事情
当delegate响应你在协议里面定义的方法的时候,就把按钮传过去(传到ViewController里面)
在这里面
-(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;
记住了,血的教训
我把cell.delegate = self;写在了 if (cell
== nil)之前
delegate是空的,我找了两个多小时,确定自己的delegate没有写错,也没有出现任何问题
高手过来一看,就说你得delagate放在 if (cell
== nil)之前,它可能是空的,所以delegate也可能是空的
把它换个位置就不会出现这样的问题了
响应delegate方法
-(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];
}
}
相关文章推荐
- iOS 自定义UITableViewCell上添加UIButton按钮实现选中的是哪个按钮对应的cell,用代理实现。
- 自定义Delegate(协议)的实现与继承
- 自定义Delegate(协议)的实现与继承
- IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)
- Qt之无边框自定义最小化、关闭按钮并实现窗口移动
- 如何实现自定义的URL协议
- Qt实现自定义按钮的三态效果
- iOS 设计中实现Cell自定义滑块操作
- UITableView 中实现 滑动一个cell 显示一个按钮,并且做相关业务处理
- 【IOS】自定义UIAlertView样式,实现可替换背景和按钮
- Android进阶——自定义View之继承系统控件实现自带删除按钮动画效果和软键盘自动悬浮于文本框下方
- 自定义带图片按钮的实现。
- TCP通讯socket自定义协议的实现
- iOS 自定义cell侧滑删除、编辑等按钮
- 在Android中实现自定义的按钮
- 自定义形状按钮的实现
- UITableView cell自定义视图中插入Table实现复杂界面
- Android自定义View实现拖动选择按钮
- 【原创】Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- iOS 自定义Cell按钮的点击代理事件