亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當選擇UITableViewCell時,您能在UITableViewCell上動畫化高度更改嗎?

當選擇UITableViewCell時,您能在UITableViewCell上動畫化高度更改嗎?

iOS
LEATH 2019-06-21 16:36:26
當選擇UITableViewCell時,您能在UITableViewCell上動畫化高度更改嗎?我用的是UITableView在我的iPhone應用程序中,我有一個屬于一個組的人的列表。我希望這樣,當用戶單擊某個特定的人(從而選擇該單元格)時,該單元格的高度將顯示多個UI控件,用于編輯該用戶的屬性。這個是可能的嗎?
查看完整描述

3 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

我找到了一個非常簡單的解決方案,作為對UITableView我在工作.

將單元格高度存儲在通常通過tableView: heightForRowAtIndexPath:,然后,當您想要動畫的高度變化,只需更改變量的值,并調用.

[tableView beginUpdates];[tableView endUpdates];

您會發現它不能完成完全的重新加載,但是對于UITableView要知道它必須重新繪制細胞,為細胞獲取新的高度值.你猜怎么著?它為你激活了變化。甜。

我有一個更詳細的解釋和完整的代碼樣本在我的博客.。動畫UITableView單元格高度更改


查看完整回答
反對 回復 2019-06-21
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

我喜歡西蒙·李的回答。我實際上沒有嘗試過這個方法,但是看起來它會改變列表中所有單元格的大小。我只是想換個被竊聽的手機。我做的有點像西蒙,但只是有點不同。這將在選定單元格時更改其外觀。它確實有生命。只是另一種方法。

創建一個int來保存當前選定單元格索引的值:

int currentSelection;

然后:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = [indexPath row];
    selectedNumber = row;
    [tableView beginUpdates];
    [tableView endUpdates];}

然后:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([indexPath row] == currentSelection) {
        return  80;
    }
    else return 40;}

我相信您可以在tableView:cellForRowAtIndexPath:更改單元格的類型,甚至為單元格加載XIB文件中進行類似的更改。

像這樣,CurentSelections將從0開始。如果不希望列表的第一個單元格(索引0)看起來是默認選中的,則需要進行調整。


查看完整回答
反對 回復 2019-06-21
?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

添加一個屬性來跟蹤所選單元格

@property (nonatomic) int currentSelection;

將其設置為一個哨位值(例如)viewDidLoad,以確保UITableView從“正常”位置開始

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //sentinel
    self.currentSelection = -1;}

在……里面heightForRowAtIndexPath可以為選定的單元格設置所需的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = self.newCellHeight;
    } else rowHeight = 57.0f;
    return rowHeight;}

在……里面didSelectRowAtIndexPath如果需要,可以保存當前選擇的內容并保存動態高度。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // do things with your cell here

        // set selection
        self.currentSelection = indexPath.row;
        // save height for full text label
        self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }}

在……里面didDeselectRowAtIndexPath將選擇索引設置為前哨值,并使單元格恢復正常形式。

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {       
        // do things with your cell here

        // sentinel
        self.currentSelection = -1;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }}


查看完整回答
反對 回復 2019-06-21
  • 3 回答
  • 0 關注
  • 746 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號