自定义TableViewCell上的按钮-找到cell行数
来源:互联网 发布:阿里巴巴大数据学院 编辑:程序博客网 时间:2024/05/22 14:27
UITableViewCell
开始的时候我不会写方法和传递参数
就全部写在
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier里面
但是后来就不太对了,他不能保存从tableView里面传过来的行数
UIButton * roomBtn =[UIButton buttonWithType:UIButtonTypeCustom];
for (int i=0; i<7; i++)
{
UIButton * roomBtn =[UIButton buttonWithType:UIButtonTypeCustom];
tag =row*7+i;
roomBtn.frame = CGRectMake(i*120+50,20,96, 96);
[roomBtn setTag:tag];
[roomBtn setTitle:[arrayobjectAtIndex:tag]forState:UIControlStateNormal];
[self.contentViewaddSubview:roomBtn];
self.selectionStyle =NO;
roomBtn.backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed:@"free"]];
[roomBtn addTarget:selfaction:@selector(TapRowBtn:)forControlEvents:UIControlEventTouchUpInside];
[roomBtn setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
}
然后执行出来就只有一个按钮,看了半天不知道为啥,自认为位置没有写错,但是所有按钮都指向了同一个位置
千万要注意,按钮的初始化要卸载for循环里面
按钮的初始化要卸载for循环里面
按钮的初始化要卸载for循环里面
因为我需要7个按钮,而不是1个
后来老师教了方法
自己写一个
-(void)customlizedWithData:(id)data findAllname:(NSArray *)array
把tableVIew的行数传到tableViewcell里面
注意
注意
-(ZMCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"cellID";
ZMCell * cell= [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
//判断cell是否为空
if (cell == nil)
{
cell = [[ZMCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
// cell重新赋值
[cell customlizedWithData:[NSNumber numberWithInt:indexPath.row] findAllname:roomsNames];
在zmcell.m里面//重新赋值
-(void)customlizedWithData:(id)data findAllname:(NSArray *)array
{
NSNumber *line = data;
row = [lineintValue];
NSLog(@"%d",row);
int tag;
BOOL isLastRow = (row +1) *7 >= array.count;
int roomNum =0;
// 不是最后一行
if (!isLastRow || array.count %7 ==0) {
roomNum =7;
}else {
//是最后一行
roomNum = array.count %7;
}
for (int i=0; i<roomNum; i++)
{
UIButton * roomBtn =[UIButtonbuttonWithType:UIButtonTypeCustom];
tag =row*7+i;
roomBtn.frame =CGRectMake(i*120+50,20,96, 96);
[roomBtnsetTag:tag];
[roomBtn setTitle:[arrayobjectAtIndex:tag]forState:UIControlStateNormal];
[self.contentViewaddSubview:roomBtn];
self.selectionStyle =NO;
roomBtn.backgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed:@"free"]];
[roomBtn addTarget:selfaction:@selector(TapRowBtn:)forControlEvents:UIControlEventTouchUpInside];
[roomBtn setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
}
接下来就是最重要的了
//重新初始化
- (void)prepareForReuse
{
//自动识别cell是否可以重用
[super prepareForReuse];
// 隐藏原有按钮
for (UIView *btninself.contentView.subviews) {
[btn removeFromSuperview];
}
}
如果没有for (UIView *btn in self.contentView.subviews) {
[btn removeFromSuperview];
}的话
每一行的按钮都会显示满
最后一行的余数也会显示满
最好的方法不是remove掉,而是隐藏掉
因为tableview的重用机制存在,
他每次remove的时候下次就要再继续生成,很浪费内存
所以应该用btn.hidden = yes;
- 自定义TableViewCell上的按钮-找到cell行数
- 在TableViewCell里的按钮事件中 获取当前Cell行数(简单做法)
- objective-C 关于找到控件处在tableviewcell上哪个cell
- ios tableviewcell上点击按钮禁用时点击走了cell点击事件的解决办法
- 自定义cell,tableviewcell
- 自定义了一个tableViewCell,cell里添加了一个按钮,怎样确定按钮所在的单元格的索引
- [iOS tableViewCell 复用] xib自定义cell 修改cell中按钮状态 出现按钮状态被复用
- UITableView自定义cell上的按钮点击事件
- iOS 点击UITableViewCell上的按钮获取当前cell和行数
- 点击tableViewCell中的一个按钮,打印cell所在的行
- tableViewCell删除按钮自定义
- tableviewcell 上放一个和cell 大小一样的button ,拦截了cell点击事件!!
- iOS自定义tableViewCell中的按钮无法点击的解决方法
- Coder_DJ 自定义tableViewCell的侧滑删除按钮
- 自定义tableView cell的删除按钮
- iOS 8自定义cell的删除按钮
- 自定义Cell中按钮的点击事件
- 自定义Cell上的按钮--delegate协议的实现
- 关于JAX-RS
- c# 解析JSON的几种办法
- adb 打印终端中内存使用情况的方法
- 使用URL传送对象到另一平台
- RAC环境下修改SGA参数(转)
- 自定义TableViewCell上的按钮-找到cell行数
- 各版本ArcGISServer安装环境【四】
- ASP.NET怎么实现新闻页面的分页功能
- SharePerference 自动登录的实现
- HSSFWorkBooK用法
- Android开发点点滴滴——关于visibility属性
- 人群与社会——第二周
- 幻世(OurDream)2D图形引擎精灵系统完成,演示下载!
- C# SQLiteHelper类似SqlHelper类实现存取Sqlite数据库