簡體   English   中英

如何使用按鈕更改uitableview中的單元格類型

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

我有兩個UIButtons作用類似於segmented control 我還具有兩個自定義UITableView cells ,並且用戶可以通過按鈕在兩種類型的單元格之間切換。 有人知道我該怎么做嗎? 這是一些代碼:

- (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;
        }
    }
}

videoCell是另一個自定義UITableViewCell ,但始終是UITableView中的第一個單元格。 有一個UIButton顯示commentCell ,一個UIButton顯示relatedCell

根據您的按鈕操作設置videoCell.buttonSelected ,調用[self.tableView reloadData]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM