2015-03-13 165 views
0

我是IOS新手,我有一個UITableview中有三個按鈕,當我選擇按鈕圖像會改變。我的問題是:如何將圖像存儲在表格視圖中,當我退出應用程序並再次回到應用程序時意味着所選圖像應該在那裏。我怎麼能做到這一過程,任何人都可以幫助我。如何在UITableview中存儲UIButton圖像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

cell = [table dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 


button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button1.frame = CGRectMake(80, 27, 36, 36); 
    [button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal]; 
    [button1 setImage:[UIImage imageNamed:@"lblue.png"] 
      forState:UIControlStateSelected]; 
    [button1 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside]; 

    button1.tag=1; 
    [cell.contentView addSubview:button1]; 
button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button2.frame = CGRectMake(160, 27, 36, 36); 
    [button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal]; 
    [button2 setImage:[UIImage imageNamed:@"eblue.png"] 
      forState:UIControlStateSelected]; 

    [button2 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:button2]; 
    button2.tag=3; 


    button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button3.frame = CGRectMake(240, 27, 36, 36); 
    [button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal]; 
    [button3 setImage:[UIImage imageNamed:@"vblue.png"] 
      forState:UIControlStateSelected]; 

    [button3 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:button3]; 
    button3.tag=2; 

- (void)radiobtn4:(UIButton *)sender 

{ 
    UITableViewCell *cell=(UITableViewCell *)sender.superview; 
    if(sender.selected == NO) 
    { 

     if(sender.tag==1){ 

      UIButton *otherButton=(UIButton *)[cell viewWithTag:1]; 
      otherButton.selected=YES; 
      UIButton *other=(UIButton *)[cell viewWithTag:2]; 
      other.selected=NO; 

     }else if(sender.tag==2){ 
      UIButton *otherButton=(UIButton *)[cell viewWithTag:2]; 
      otherButton.selected=YES; 
      UIButton *other=(UIButton *)[cell viewWithTag:1]; 
      other.selected=NO; 


     } else if(sender.tag == 3) 
     { 
      UIButton *otherButton=(UIButton *)[cell viewWithTag:3]; 
      otherButton.selected=YES; 

     } 

    } else if(sender.selected == YES) 

    { 
     if(sender.tag==1){ 
      UIButton *otherButton=(UIButton *)[cell viewWithTag:1]; 
      [otherButton setImage:[UIImage imageNamed:@"l.png"] 
         forState:UIControlStateNormal]; 
      otherButton.selected=NO; 


     }else if(sender.tag==2){ 
      UIButton *otherButton=(UIButton *)[cell viewWithTag:2]; 
      [otherButton setImage:[UIImage imageNamed:@"v.png"] 
         forState:UIControlStateNormal]; 
      otherButton.selected=NO; 

     } else if(sender.tag == 3) 
     { 
      UIButton *otherButton=(UIButton *)[cell viewWithTag:3]; 
      [otherButton setImage:[UIImage imageNamed:@"e.png"] 
         forState:UIControlStateNormal]; 
      otherButton.selected=NO; 

     } 

    } 

} 

上面是我的編碼。

+1

使用數據庫或plist中存儲你的選擇.. – iphonic 2015-03-13 09:52:19

+0

主要標誌陣列/字典你逝去/使用 – the1pawan 2015-03-13 09:56:57

回答

0

使用NSUserDefaults

儲存:

[[NSUserDefaults standardUserDefaults] setObject:imageName forKey:@"selected_image"]; 

檢索:

NSString *imageName = [[NSUserDefaults standardUserDefaults] stringForKey:@"selected_image"]; 

*後來編輯

在你的情況,你應該保存圖像名稱中radiobtn4:和水庫撕毀在cellForRowAtIndexPath:

例:

- (void)radiobtn4:(UIButton *)sender{ 
    .... 

    if(sender.tag==1){ 
     ... 
     [[NSUserDefaults standardUserDefaults] setObject:imageName forKey:@"selected_image1"]; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    [button1 setImage:[UIImage imageNamed:[[NSUserDefaults standardUserDefaults] stringForKey:@"selected_image1"];] 
     forState:UIControlStateNormal]; 
    ... 
} 
+0

是我應該添加此 – Raj 2015-03-13 10:04:25

0

您需要將表中每個條目的選定狀態存儲到您的數據模型中。另一張海報提出了幾種不同的方式來保存這些數據。

如何保存顯示在表格視圖中的其他信息?我建議你在一個單元格的當前數據中加入一個字段,告訴你選擇了哪個按鈕。最好將每個條目的所有信息保存在一起,而不是創建一個新的結構,以便在其他地方保存。

然後在你的cellForRowAtIndexPath選擇基於該信息正確的按鈕。

現在你可能有一個問題。如果您更改最上方單元格中的選定按鈕,請向下滾動表格視圖中的某個方向,然後滾動回頂部,頂部按鈕可能會丟失其選定的按鈕狀態。您甚至可能在滾動到視圖中顯示錯誤的選定按鈕狀態的單元格中遇到問題。這是因爲細胞被回收利用,您必須完全配置一個細胞,或者從上次使用細胞時開始剩餘設置。