简体   繁体   English

如何动态地将UIButton添加到UITableViewCell? UIButton可以是一个或多个然后是一个

[英]How to add UIButton to UITableViewCell dynamically? UIButton may be one or more then one

webi7,webi5,webi2 are users likes my uploaded video these all are button it may be 10 or 1 name of users who likes my video. webi7,webi5,webi2是喜欢我上传的视频的用户,这些都是按钮,喜欢我视频的用户可能是10或1。

I want to add uibutton. 我想添加uibutton。 and after click on that user button it displays its detail screen same as instagram app. 在点击该用户按钮后,它显示其详细信息屏幕与instagram应用相同。

Currently its working fine with ios 6.0 but gets crash in 5.0 目前,它可以在ios 6.0上正常工作,但在5.0中崩溃

Here is my Table's Custom Cell code : 这是我表的自定义单元格代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoListCustomCell *cell = (VideoListCustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"VideoListCustomCell"];
    //if(cell == nil)    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"VideoListCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.showsReorderControl = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];
        [cell drawImageView];

        [cell.btnLike addTarget:self action:@selector(btnLikeClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnComment addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnOther addTarget:self action:@selector(btnOtherClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnUserImage addTarget:self action:@selector(btnUserImageClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnUserName addTarget:self action:@selector(btnUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnVideo addTarget:self action:@selector(btnVideoClick:) forControlEvents:UIControlEventTouchUpInside];
    //}

    VideoShare *shareObj = [arrVideos objectAtIndex:indexPath.row];

    [cell.btnUserName setTitle:shareObj.vdUserName forState:UIControlStateNormal];
    cell.btnUserImage.imageURL = [NSURL URLWithString:shareObj.vdUserPhotoUrl];
    cell.btnVideoThumbnail.imageURL = [NSURL URLWithString:shareObj.vdThumbUrl];
    cell.lblTimeUpload.text=shareObj.vdTimeUpload;

    cell.btnComment.tag = shareObj.vdId;
    cell.btnLike.tag = indexPath.row;
    cell.btnOther.tag = indexPath.row;
    cell.btnUserImage.tag = shareObj.vdUserId;
    cell.btnUserName.tag = shareObj.vdUserId;
    cell.btnVideo.tag = indexPath.row;

    int posx = 18;
    int posy = 0;

    if (shareObj.vdLoginUserLikeOrNot == 1) {
        [cell.btnLike setTitle:@"Liked" forState:UIControlStateNormal];
    }
    else {
        [cell.btnLike setTitle:@"Like" forState:UIControlStateNormal];
    }

    if ([shareObj.vdLikeUserDetail count] == 0) {
        cell.imgLikeSymbol.hidden = YES;
    }
    else if ([shareObj.vdLikeUserDetail count] > 4) {
        cell.imgLikeSymbol.hidden = NO;

        UIButton *btnLikeUserCount = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        btnLikeUserCount.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btnLikeUserCount.frame = CGRectMake(posx,posy,200,16);
        btnLikeUserCount.tag = shareObj.vdId;
        [btnLikeUserCount setTitle:[NSString stringWithFormat:@"%d likes",shareObj.vdLikeCount] forState:UIControlStateNormal];
        [btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
        [btnLikeUserCount addTarget:self action:@selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.viewLikeComment addSubview:btnLikeUserCount];
        posy = posy + 22;
    }
    else {
        cell.imgLikeSymbol.hidden = NO;

        NSMutableArray *arrLikeUserDetail = shareObj.vdLikeUserDetail;
        for (int i = 0; i<[arrLikeUserDetail count]; i++) {
            UserShare *shareObjU = [arrLikeUserDetail objectAtIndex:i];
            NSString *text;
            if (i == [arrLikeUserDetail count]-1) {
                text = shareObjU.userName;
            }
            else {
                text = [NSString stringWithFormat:@"%@,",shareObjU.userName];
            }
            CGSize constraint = CGSizeMake(280, 2000);
            CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
            UIButton *btnLikeUserName = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnLikeUserName.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];

            if (posx + size.width > 280) {
                posy = posy + 22;
                posx = 18;
                btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
                posx = posx + size.width + 5;
            }
            else {
                btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
                posx = posx + size.width + 5;
            }
            btnLikeUserName.tag = shareObjU.userId;
            [btnLikeUserName setTitle:text forState:UIControlStateNormal];
            [btnLikeUserName setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
            [btnLikeUserName addTarget:self action:@selector(btnLikeUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.viewLikeComment addSubview:btnLikeUserName];
        }
        posy = posy + 22;
    }

    cell.imgCommentSymbol.frame = CGRectMake(0,posy,15,15);
    posx = 18;

    NSMutableArray *arrCommentDetail = shareObj.vdCommentDetail;
    if ([arrCommentDetail  count] == 0) {
        cell.imgCommentSymbol.hidden = YES;
    }
    else if ([arrCommentDetail  count] > 3) {
        cell.imgCommentSymbol.hidden = NO;
        for (int i = 0; i<3; i++) {
            CommentShare *shareObjC = [arrCommentDetail objectAtIndex:i];
            NSString *text = shareObjC.userName;
            CGSize constraint = CGSizeMake(280, 2000);
            CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
            UIButton *btnCommentUserName = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnCommentUserName.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
            btnCommentUserName.frame = CGRectMake(posx,posy,size.width,size.height);
            btnCommentUserName.tag = shareObjC.userId;
            [btnCommentUserName setTitle:text forState:UIControlStateNormal];
            [btnCommentUserName setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
            [btnCommentUserName addTarget:self action:@selector(btnCommentUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.viewLikeComment addSubview:btnCommentUserName];

            posx = posx + size.width + 5;
            NSString *text1 = shareObjC.commentText;
            CGSize constraint1 = CGSizeMake(280 - (size.width + 5), 2000);
            CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
            UILabel *lblComment = [[[UILabel alloc] initWithFrame:CGRectMake(posx,posy,size1.width,size1.height)] autorelease];
            lblComment.lineBreakMode = UILineBreakModeWordWrap;
            lblComment.numberOfLines = size1.height/15;
            [lblComment setFont:[UIFont systemFontOfSize:12]];
            lblComment.text = text1;
            [cell.viewLikeComment addSubview:lblComment];
            posx = 18;
            posy = posy + size1.height + 2;
        }
        UIButton *btnCommentViewAll = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnCommentViewAll.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
        btnCommentViewAll.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btnCommentViewAll.frame = CGRectMake(posx,posy,200,16);
        btnCommentViewAll.tag = shareObj.vdId;
        [btnCommentViewAll setTitle:[NSString stringWithFormat:@"View all %d Comments",arrCommentDetail.count] forState:UIControlStateNormal];
        [btnCommentViewAll setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btnCommentViewAll addTarget:self action:@selector(btnCommentViewAllClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.viewLikeComment addSubview:btnCommentViewAll];
        posx = 18;
        posy = posy + 16 + 2;
    }
    else {
        cell.imgCommentSymbol.hidden = NO;
        for (int i = 0; i<[arrCommentDetail count]; i++) {
            CommentShare *shareObjC = [arrCommentDetail objectAtIndex:i];
            NSString *text = shareObjC.userName;
            CGSize constraint = CGSizeMake(280, 2000);
            CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
            UIButton *btnCommentUserName = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnCommentUserName.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
            btnCommentUserName.frame = CGRectMake(posx,posy,size.width,size.height);
            btnCommentUserName.tag = shareObjC.userId;
            [btnCommentUserName setTitle:text forState:UIControlStateNormal];
            [btnCommentUserName setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
            [btnCommentUserName addTarget:self action:@selector(btnCommentUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.viewLikeComment addSubview:btnCommentUserName];

            posx = posx + size.width + 5;
            NSString *text1 = shareObjC.commentText;
            CGSize constraint1 = CGSizeMake(280 - (size.width + 5), 2000);
            CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
            UILabel *lblComment = [[[UILabel alloc] initWithFrame:CGRectMake(posx,posy,size1.width,size1.height)] autorelease];
            lblComment.lineBreakMode = UILineBreakModeWordWrap;
            lblComment.numberOfLines = size1.height/15;
            [lblComment setFont:[UIFont systemFontOfSize:12]];
            lblComment.text = text1;
            [cell.viewLikeComment addSubview:lblComment];
            posx = 18;
            posy = posy + size1.height + 2;
        }
    }

    cell.viewLikeComment.frame = CGRectMake(10,340,300,posy);
    cell.btnLike.frame = CGRectMake(10,340+posy+5,80,20);
    cell.btnComment.frame = CGRectMake(100,340+posy+5,80,20);
    cell.btnOther.frame = CGRectMake(260,340+posy+5,50,20);
    return cell;
}

在init方法的VideoListCustomCell中添加按钮。

您可以尝试使用:UIButton * obj = [UIButton alloc] init];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM