简体   繁体   中英

How can I change the type of cell in uitableview with a button

I have two UIButtons that act like a segmented control . I also have two custom UITableView cells , and I the user to be able to toggle between the two types of cells with the buttons. Does anyone know how I can do this? Here is some code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *videoCellID = @"videoCellID";
    static NSString *commentCellID = @"commentCellID";
    static NSString *relatedCellID = @"relatedCellID";

    VideoDetailCell *videoCell;

    if (indexPath.row == 0)
    {
        videoCell = (VideoDetailCell *)[tableView dequeueReusableCellWithIdentifier:videoCellID];
        if (!videoCell)
        {
            videoCell = [VideoDetailCell generateCellWithInfo:nil];
            videoCell.navigation = self.navigationController;
        }
        return videoCell;
    }
    else
    {
        if (videoCell.buttonSelected == 0)
        {
            self.cellType = 0;
            CommentCell *commentCell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:commentCellID];
            if (!commentCell)
            {
                commentCell = [CommentCell generateCellWithInfo:nil];
            }
            return commentCell;
        }
        else
        {
            self.cellType = 1;
            RelatedCell *related = (RelatedCell *)[tableView dequeueReusableCellWithIdentifier:relatedCellID];
            if (!related)
            {
                related = [RelatedCell generateCellWithInfo:nil];
            }
            return related;
        }
    }
}

The videoCell is another custom UITableViewCell , but that will always be the first cell in the UITableView. There is a UIButton to show the commentCell , and one to show the relatedCell .

根据您的按钮操作设置videoCell.buttonSelected ,调用[self.tableView reloadData]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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