简体   繁体   中英

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!

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:

 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:

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:

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).

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;

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.

if in .h fileyou are having an object of TableView

UITableView *table;

Then when you want to update your table you should write,

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

  2. Save it into String

  3. Access this string into new View Controller, where your UITableView delegates methods are implemented

  4. cell.textLabel.text = str ; where str is the string which you selected on your pickerView

  5. In a method of new View Controller, where you are accessing string, write a line of code [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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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