简体   繁体   English

如何根据选择器视图中选择的行组件更新表视图数据

[英]How to update table view data based on row component selected in picker view

I have an app,where I need to save a reminder in one page and in other display the saved reminder.I have done it successfully using sqlite3! 我有一个应用程序,我需要在一个页面中保存提醒,在其他显示中保存已提醒的提醒。我已成功使用sqlite3!

In the view reminder page,the user would have 3 options to view,view all which I have already implemented,view group that contains reminders of 4 groups.Because they are just 4 groups,I have created 4 view controllers for 4 unique groups.Just simply using the following query which worked perfect: 在视图提醒页面中,用户可以查看3个选项,查看我已经实现的所有选项,查看包含4组提醒的组。由于它们只是4组,我为4个唯一组创建了4个视图控制器。只需使用以下完美的查询:

 NSString *sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE grp = 'Friends'"];

But I have a problem with view monthly page which contains 12 row components in picker view for the user to select,ie all the 12 months of the year: 但我查看每月页面有一个问题,其中包含选择器视图中的12个行组件供用户选择,即一年中的所有12个月:

I have found out the query for retrieving all the reminders in a particular month,say for month december,I have retrieved using the following query: 我找到了检索特定月份中所有提醒的查询,比如12月份,我使用以下查询检索:

NSString *sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE Date BETWEEN '2012-12-01' AND '2012-12-31'"];

Before entering in to view monthly page(controller),there is a month picker view as I have mentioned earlier,I know that we can have separate view controller pages for displaying the data of 12 months as I did in the case of view group reminders,but the code becomes hectic.Hence I want the table view to get updated automatically based on month selected in picker view(row component). 在进入查看每月页面(控制器)之前,有一个月选择器视图,如前所述,我知道我们可以有单独的视图控制器页面来显示12个月的数据,就像我在视图组提醒的情况下那样,但代码变得忙乱。我希望表视图根据选择器视图(行组件)中选择的月份自动更新。

I found out a similar question,but the question was unanswered! 我发现了一个类似的问题,但问题没有答案!

Update table view data based on row component selected in picker view 根据选择器视图中选择的行组件更新表视图数据

I tried to create an instance of the class controller where picker view is present for the view controller where data is getting displayed: 我试图创建一个类控制器的实例,其中视图控制器存在选择器视图,其中显示数据:

ERViewEditController *viewEditController; ERViewEditController * viewEditController;

and while retrieving I used the following code: 在检索时我使用了以下代码:

if(viewEditController.monthPicker selectedRowInComponent:0)
{
sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE Date BETWEEN '2012-01-01' AND '2012-01-31'"];
sql_stmt = [sqlQuery UTF8String];
}

else if(viewEditController.monthPicker selectedRowInComponent:1)
{
sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE Date BETWEEN '2012-02-01' AND '2012-02-31'"];
sql_stmt = [sqlQuery UTF8String];
}

etc... and so on for all months then used the following code for display: 等...等所有月份都使用以下代码进行显示:

if (sqlite3_prepare_v2(self.remindersDB, sql_stmt, -1, &statament, NULL) == SQLITE_OK) 
        {
            while (sqlite3_step(statament) == SQLITE_ROW) 
            {             
                ReminderClass *remin = [[ReminderClass alloc]init];

                remin.Name = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 1)]autorelease];
                remin.Event = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 2)]autorelease];
                remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

                NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
                [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSDate *date = [dateFormat dateFromString:remin.Date];
                [dateFormat setDateFormat:@"MMMM dd"];
                NSString *dateVal = [dateFormat stringFromDate:date];
                remin.Date = dateVal;

                [self.monthArray addObject:remin];
                [remin release];
            }

            sqlite3_finalize(statament);
        }

Nothing is getting displayed now :( 现在没有显示任何内容:(

How can I achieve the requirement,can any one please help me out with your valuable suggestions! 如何达到要求,任何人都可以帮助我提出宝贵的建议!

Thanks all in advance :) 在此先感谢所有:)

You should just call reloadData method. 你应该只调用reloadData方法。

if in .h fileyou are having an object of TableView 如果在.h文件中你有一个TableView对象

UITableView *table;

Then when you want to update your table you should write, 然后,当你想要更新你的表时,你应该写,

[table reloadData];
  1. Select the Row of PickerView 选择PickerView行

  2. Save it into String 将其保存为String

  3. Access this string into new View Controller, where your UITableView delegates methods are implemented 将此字符串访问到新的View Controller中,您可以在其中实现UITableView委托方法

  4. cell.textLabel.text = str ; cell.textLabel.text = str ; where str is the string which you selected on your pickerView 其中str是您在pickerView上选择的字符串

  5. In a method of new View Controller, where you are accessing string, write a line of code [table reloadData]; 在新的View Controller方法中,您要访问字符串,写一行代码[table reloadData];

Debug you code by inserting debug Point at line. 通过在行插入调试点来调试代码。

[self.monthArray addObject:remin];

then,you should write the following code to conform that either your array is empty or having required values. 那么,您应该编写以下代码以符合您的数组为空或具有所需的值。

for(int i=0;i<[self.monthArray count];i++)
{
NSLog("Array ids :%@",[self.monthArray ObjectAtIndex:i])
}

if it prints the correct values in console, then you should write the following line in the delegate method cellForRowAtIndex. 如果它在控制台中打印正确的值,那么您应该在委托方法cellForRowAtIndex中编写以下行。

cell.textLabel.text = [self.monthArray objectAtIndex:indexPath.row];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM