iOS开发笔记-cell里点击按钮展开收起-动态改变cell行高

来源:互联网 发布:c语言for语句continue 编辑:程序博客网 时间:2024/06/05 08:15

1.cell for row

<span style="white-space:pre"><span style="background-color: rgb(255, 255, 255);"></span></span><span style="background-color: rgb(255, 255, 255);">//配置cell</span>
<span style="white-space: pre;"></span><span style="background-color: rgb(255, 255, 255);">[cell congigCell:cell.dic];</span>
<span style="background-color: rgb(255, 255, 255);"><span style="white-space:pre"></span>//下拉展开按钮                cell.ZhankaiBtn.tag = indexPath.row+1000;                [cell.ZhankaiBtn addTarget:self action:@selector(zhankai:) forControlEvents:UIControlEventTouchUpInside];                cell.selectionStyle = UITableViewCellSelectionStyleNone;                return cell;</span>
<span style="background-color: rgb(255, 255, 255);">2.按钮点击事件</span>
<span style="font-size:10px;background-color: rgb(255, 255, 255);"></span><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">-(void)zhankai:(UIButton *)btn{</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">    <span style="white-space:pre"></span>NSMutableDictionary *dic;</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">    <span style="white-space:pre"></span>btn.selected = !btn.selected;</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"> <span style="white-space:pre"></span>dic = [NSMutableDictionary dictionaryWithDictionary:self.valueArray[btn.tag-1000]];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">//1 2 标识一种状态 1代表展开状态 2代表闭合状态</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">        if ([[dic safeNullWithKey:@"iszhankai"] isEqualToString:@"2"]) {</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; min-height: 21px;">            [dic setObject:@"1" forKey:@"iszhankai"];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">        }else if ([[dic safeNullWithKey:@"iszhankai"] isEqualToString:@"1"]) {</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">            [dic setObject:@"2" forKey:@"iszhankai"];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">        }else{</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">            [dic setObject:@"1" forKey:@"iszhankai"];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">        }</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;">        [self.valueArray replaceObjectAtIndex:btn.tag-1000 withObject:dic];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo;"> <span style="white-space:pre"></span>NSIndexPath *path = [NSIndexPath indexPathForRow:btn.tag-1000 inSection:1];</p><p style="margin-top: 0px; margin-bottom: 0px; line-height: normal; font-family: Menlo; min-height: 21px;">   <span style="white-space:pre"></span>[self.ev_tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];</p>
2.height for row

//这里我用一个方法统一来计算高度

        return [calcuH cellHeight:dic];

//-----------------------------------------------

-(CGFloat)cellHeight:(NSDictionary *)dic{

    //45 26+

    CGFloat heightt = 45+26+5;

    /*评价内容*/ //在我的另一篇文章里有这个方法,地址:http://blog.csdn.net/jp940110jpjp/article/details/42262021

    CGSize labContextWidth = [calculatorStringSize stringSize:[dic objectForKey:@"evaluateContext"] widthOfFatherView:WIDTH-20 textFont:[UIFont systemFontOfSize:11.0]];

    

    if ([[dic safeNullWithKey:@"iszhankai"] isEqualToString:@"1"]) {

        heightt+= labContextWidth.height;

   }else{

        if (labContextWidth.height>=30) {

            heightt+= 30+10+14;

        }else{

            heightt+= labContextWidth.height;

        }

    }

    CGFloat imgVWidth = (SCREEN_WIDTH-26-18) /4.0;

    NSArray *imageArray = dic[@"evaluateIcons"];

    if (imageArray.count>=1) {

        heightt+= 10+imgVWidth+15+12+20;

    }else{

        heightt+= 10+15+12+20;


    }

    

    return heightt;

}


OK,大致这些,写的很粗糙,还有很多需要优化的地方,cell里的配置方法,根据dic里的@"iszhankai"参数进行相应的配置就行,有好的建议欢迎大家提出来

0 0
原创粉丝点击