Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableview中存储UIButton图像_Ios_Objective C_Iphone_Uitableview_Uibutton - Fatal编程技术网

Ios 如何在UITableview中存储UIButton图像

Ios 如何在UITableview中存储UIButton图像,ios,objective-c,iphone,uitableview,uibutton,Ios,Objective C,Iphone,Uitableview,Uibutton,我是IOS新手,我有一个UITableview,有三个按钮,当我选择按钮时,图像会改变。我的问题是:如何在表视图中存储图像,当我退出应用程序并再次返回应用程序时,意味着所选图像应该存在。我怎样才能做到这一点?谁能帮助我呢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSStri

我是IOS新手,我有一个UITableview,有三个按钮,当我选择按钮时,图像会改变。我的问题是:如何在表视图中存储图像,当我退出应用程序并再次返回应用程序时,意味着所选图像应该存在。我怎样才能做到这一点?谁能帮助我呢

- (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;

        }

    }

}

以上是我的编码。

使用
NSUserDefaults

存储:

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

NSString *imageName = [[NSUserDefaults standardUserDefaults] stringForKey:@"selected_image"];
*以后编辑

在您的情况下,您应该将图像名称保存在
radiobtn4:
中,并在
cellforrowatinexpath:

例:


您将使用以下方法执行此操作,请选择其中一种。 如果您只需要保存按钮状态,我建议使用NSUserDefaults,其他vise使用CoreData

使用NSUserDefaults

使用CoreData

使用Plist读/写
使用web服务您需要将表中每个条目的选定状态存储到数据模型中。另一张海报提出了几种不同的保存数据的方法

如何保存表视图中显示的其他信息?我建议您在单元格的当前数据中添加一个字段,告诉您选择了哪个按钮。最好将每个条目的所有信息保存在一起,而不是创建一个保存在其他地方的新结构

然后在cellForRowAtIndexPath中,根据该信息选择正确的按钮


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

使用数据库或plist来存储您的选择..您要传递的数组/字典中的主标志/usingware我应该添加它吗
- (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];
    ...
}