UITableView與UIAlertView的 Delegate方法實現,uitableviewdelegate

來源:互聯網
上載者:User

UITableView與UIAlertView的 Delegate方法實現,uitableviewdelegate

一 UITableView Delegate 方必須遵循 UITableViewDelegate協議

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 這句是定義cell右邊的尖角號

#pragma mark - 代理方法

#pragma mark 返回indexPath這行cell的高度

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

{

//    return 70 + indexPath.row * 20;

    return 70;    該方法直接返回cell的高度 

 

}

#pragma mark 選中了某一行的cell就會調用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath//本句實現點擊行要執行的方法

{

    // 0.取出所點擊這行的產品對象

    Shop *shop = _shops[indexPath.row];

    // 1.建立彈框

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"產品資訊展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

    // 設定樣式(一個明文文字框)

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    // 設定文字框的預設文字

    [alert textFieldAtIndex:0].text = shop.name;

        // 2.顯示彈框

    [alert show];

    // 3.綁定行號 為 alertview的tag

    alert.tag = indexPath.row;

}

二 UIAlertView 的Delegate方法

  // 2.2.重新整理表格

//    [_tableView reloadData]; // 整體重新整理(每一行都會重新整理)

    

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];

    

    NSArray *paths = @[indexPath];

    [_tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

#pragma mark - alertview的代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex//監聽確認按鈕,實現修改方法,通過傳UIAlertView tag

{

    if (buttonIndex == 0) return;

    

    // 1.取出文字框的文字

    NSString *text = [alertView textFieldAtIndex:0].text;

    

    // 2.將文字更新到對應的cell上面去

    

    // 2.1.修改對應位置的模型資料

    int row = alertView.tag; // alertView的tag就是行號

    Shop *shop = _shops[row];

    shop.name = text;

    

    // 2.2.重新整理表格

//    [_tableView reloadData]; // 整體重新整理(每一行都會重新整理)

    

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];

    

    NSArray *paths = @[indexPath];

    [_tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

    

/*

 重新向資料來源索取資料

 重新向資料來源發送訊息

 重新調用資料來源的方法,根據傳回值決定顯示什麼資料

 */

    

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.