2015-03-13 168 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选择基于该信息正确的按钮。

现在你可能有一个问题。如果您更改最上方单元格中的选定按钮,请向下滚动表格视图中的某个方向,然后滚动回顶部,顶部按钮可能会丢失其选定的按钮状态。您甚至可能在滚动到视图中显示错误的选定按钮状态的单元格中遇到问题。这是因为细胞被回收利用,您必须完全配置一个细胞,或者从上次使用细胞时开始剩余设置。