【问题标题】:Store button with image in to sqlite3 database将带有图像的按钮存储到 sqlite3 数据库中
【发布时间】:2012-01-09 15:08:30
【问题描述】:

我有一个包含文本字段和文本视图作为子视图的表单(表格视图),我需要保存这些字段中填写的数据。因此我使用了 sqlite3。一切正常,保存检索和显示保存的数据。但在表中视图,我有一个单元格,其中包含一个按钮作为子视图。我们知道在 iphone 中没有称为复选框按钮的概念。因此我使用按钮并添加了默认图像作为子视图,如下所示:

未选中按钮时,图片不变。

现在,如果用户选择按钮,图像将变为复选标记,表示逻辑每年都有效,如下所示:

现在我的问题是如何将按钮图像保存到 sqlite3。我使用以下查询插入值:

NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,fieldSix.text,textView.text,fieldSeven.text];

同样,如何在 sqlite3 中将带有图像的按钮保存为子视图。我听说 sqlite3 中没有像 BOOL 值存储这样的概念。相反,它存储为 int 值,即“0”为假,“1”为真.因此我使用以下查询插入按钮图像:

int selValue;

if(checkboxSelected == NO)
{
    selValue = 0;
}

else if(checkboxSelected == YES)
{
    selValue = 1;
}
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,val,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,isSelected,fieldSix.text,textView.text,fieldSeven.text]; 

在控制台中,如果按钮被选中,则 selValue 显示为 1,否则显示为 0。

但我有一个名为查看编辑提醒的控制器页面,用户可以在其中查看保存的提醒并编辑发生的更改。现在我保存了一个名为 Amma 的提醒,在保存时我选择了每年按钮,但它即使为提醒检索插入的数据,也无法正常工作,如下所示:

编辑:

我为从 sqlite3 数据库表中检索数据而实现的代码:

ReminderClass *remind = [[ReminderClass alloc]init];
                remind.Name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 1)];
                remind.Event = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 2)];
                remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];

                remind.time = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 4)]; 
                remind.numDays = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 5)];
                remind.selString = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 6)]];
                remind.number = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 7)];
                remind.msgBody = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 8)];
                remind.remGroup = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 9)];

编辑:

这是我为编辑内容所做的:

-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (aTable.editing) 
    {
        ReminderClass *rem = (ReminderClass *)[self.monthArray objectAtIndex:indexPath.section];

        // Instantiate your detail/editor view controller,
        // and pass in the ReminderClass object to be edited.
        ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
        [self.navigationController pushViewController:rdvc animated:YES];
    }

    [atableView deselectRowAtIndexPath:indexPath animated:YES];
}

请帮我提出宝贵的建议

提前谢谢大家:)

【问题讨论】:

  • 你的sqllite数据库中是否存储了年份选择值0或1?
  • @hiren443 是的,我已经存储了选择值
  • 显示您编辑剩余部分的显示代码
  • 是的,我已经发布了当表格视图处于编辑模式时用于检索数据的代码,remind.selString 是导致我出现问题的原因

标签: iphone sqlite insert uibutton


【解决方案1】:

设置:

int selValue;

if(checkboxSelected == NO)
{
    selValue = 0;
}

else if(checkboxSelected == YES)
{
    selValue = 1;
}


       NSNumber *number = [NSNumber numberWithInt:selValue]; 
    NSString *isSelected = [number stringValue];


 NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,val,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,isSelected,fieldSix.text,textView.text,fieldSeven.text];

获取:

 remind.selString = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 6)];

 NSString *one = remind.selString;
     NSNumber* i = [NSNumber numberWithInt:[one intValue]];;
     BOOL isOn = [i boolValue];

if(isOn){
   //set ON Image
}
else{
   //set OFF image
}

【讨论】:

  • 感谢您的回答,但我有疑问。表中用于保存值的语法是什么。我的意思是 CREATE TABLE 提醒(id INTEGER PRIMARY KEY,Name VARCHAR(64) ,button ?),问号是我需要的,另外在插入时我们使用INSERT INTO提醒((name,event,button) VALUES (\"%@\", \"%@\ ”,?)。按钮的格式是否也相同,需要一些澄清:P提前感谢:)
  • 我不清楚你为什么要这样做 :isEqualToString:@"YES"];。你不能像其他的一样将值作为字符串值传递吗? @"1",@"2"?然后就可以一样了,按钮VARCHAR(64)
  • 哦,非常感谢,但我想要@"0" || @"1",我的意思是检查按钮是否被选中,请在这方面帮助我:)
  • 查看我编辑的代码,展示如何将 NSInteger 转换为字符串。
  • 我应该设置什么开/关图像
【解决方案2】:

如果你得到了reminder.selString 然后把它保存在

NSString *str = [NSString stringWithFormat:@"%@", remind.selString];

然后检查您的状态

if([str isEqualToString:@"0"]){
// put your code here to set your images 
}
else{

}

【讨论】:

  • 这是我在编辑(导航到控制器)之前必须检查的代码,其中保存了提醒实例的数据
  • 你必须在显示整个页面之前编写。就像你得到数据后你必须做这个功能
  • 实际上Hubert Kunnemeyer先生给出的答案是有效的。因此我再次感谢它的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多