【UIKit】UITableView.04

來源:互聯網
上載者:User

標籤:des   style   blog   http   使用   io   strong   檔案   

UITableView.04:

 

【1】拖入一個UITableView

【2】將TableView的dataSource與控制器串連

【3】首先得遵循UITableView的資料來源協議<UITableViewDataSource>

【4】加入表徵圖檔案

【5】代碼

1.建立一個Product類,用來作為對象內容表示產品資訊

  

2.在Product.h中添加聲明代碼

@interface Product : NSObject/*********設定產品內容資訊*********//* 圖片*/@property (nonatomic,strong)NSString *icon;/* 產品名稱*/@property (nonatomic,strong)NSString *name;/* 產品描述(不能寫description)*/@property (nonatomic,strong)NSString *desc;@end

3.建立1個空個數組,建立30個Product,分別加入產品資訊,然後加入數組。

- (void)viewDidLoad{    [super viewDidLoad];    // 空的數組    NSMutableArray *data=[NSMutableArray array];    for(int i=0;i<30;i++)    {        /**********建立30個product************/        Product *p=[[Product alloc] init];        // 產品名        p.name=[NSString stringWithFormat:@"產品-%d",i];        // 產品描述        p.desc=[NSString stringWithFormat:@"%@好好好好!!!!",p.name];

/**********隨機插入圖片****************/ // 建立1-9的隨機數 int index=arc4random_uniform(8)+1; p.icon=[NSString stringWithFormat:@"00%d.png",index]; /**********加入數組*******************/ [data addObject:p]; } // 賦值 self.data=data;}

4.返回一共有多少行

#pragma  mark 資料來源方法-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.data.count;}

5.返回每一行顯示的資料內容

#pragma  mark 返回每一行顯示的資料-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    /**********取出這行對應的Product對象************/   // 建立一個product指向這一行    Product *p=self.data[indexPath.row];        // 加入標題    cell.textLabel.text=p.name;    // 加入內容    cell.detailTextLabel.text=p.desc;    //加入圖片    cell.imageView.image=[UIImage imageNamed:p.icon];    return cell;}

6.要使用點擊一下,就會顯示一個Alert,就需要使用代理

  1)連線

    

  2)加入協議<UITableViewDelegate>

  3) 代碼

#pragma mark -代理方法#pragma mark 該方法是選中某一行就會調用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 1.獲得被點擊這行對應的產品資訊    Product *p = self.data[indexPath.row];        // 彈窗    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"產品資訊"                                                   message:nil                                                 delegate:self                                        cancelButtonTitle:@"取消"                                        otherButtonTitles:@"確定", nil];        /****儲存點擊的行號給下面的Alert方法使用********/// 這一行實際效果不在這一行。    alert.tag = indexPath.row;        // alert 樣式設定    alert.alertViewStyle=UIAlertViewStylePlainTextInput;        // 取出文本輸入框,將常值內容賦值給輸入框    [alert textFieldAtIndex:0].text=p.name;        // alert 調用show方法顯示對話方塊    [alert show];        NSLog(@"第%d行",indexPath.row);}

 

7.將彈出來的文字框中的“確定”按鈕能真正修改,需要使用協議

  1) 添加協議<UIAlertViewDelegate>

  2) 1.擷取到文字框中內容

      2.將內容先更新到對象數組

    3.將數組進行局部更新或者全域更新

#pragma mark -AlertView 代理方法#pragma mark 點擊了AlertView的某個按鈕時調用-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if(buttonIndex==1) //點擊了確定按鈕    {    //1.取得文字框的文字        NSString *name= [alertView textFieldAtIndex:0].text;
// 2. 顯示到對應的行上去使用MVC
/***********更新資料************/ //(alertView,tag 就是行號) Product *p=self.data[alertView.tag]; // 修改對象,只改掉模型,只能通過下面的重新整理後顯示 p.name=name;
//3.重新整理介面 /*重新載入資料 本質:重新向資料來源請求資料(重新調用資料來源的回應程式法) */ /*******局部更新*******/ //表示的是哪一行 NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0]; //重新整理單行,並且有動畫 [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft]; /*******這個是全部更新******/ //[self.tableView reloadData];// 這個是全部更新 }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.