开发者

Use of localizablestrings to satisfy a particular condition and display the particular string

开发者 https://www.devze.com 2023-03-24 09:55 出处:网络
I have an application in which i have two tableviewcontroller. In the first tableview controller when the second row is selected a new tableview isloaded with 7 rows in it.

I have an application in which i have two tableview controller. In the first tableview controller when the second row is selected a new tableview is loaded with 7 rows in it.

The value in the rows of this tableview is populated by using an NSMutableArray in which statically I have stored 7 values in it and then I have returned the array in the no of rows method. In the didselect method of this controller I am calling a method which calls the localizable.strings file and depend upon my condition return the string on the detailtext of my first tableview controller. In the second controller I have tableview with 7 rows each row with value "Every Monday","Every Tuesday","Every Wednesday","Every Thursday","Every Friday","Every Saturday","Every Sunday".

  1. When I click on particular row the value of that particular row must be displayed on the detailtext of first controller.

  2. When I click on first 5 rows i.e from Monday to Friday a value of "Every Weekdays" must be displayed on the detailtext of first controller.

  3. When I click on the last 2 rows i.e Saturday & Sunday a value of "Every Weekends" must be displayed on the detailtext of first controller.

  4. When I click on any random rows i.e Monday, Wednesday, Thursday a value of "Every Mon,Wed,Thu" must be displayed on the detailtext of first controller.

This is my code of second controller where I select my cell:

#import "TAlarmNewController.h"
//#import "global.h"
#import "TAddAlarmController.h"
#import "global.h"
#import "Alarm.h"
#import <sqlite3.h>
#define DATABASE_NAME @"Alarmcheck.sqlite"
#define DATABASE_TITLE @"Alarmcheck"

@implementation TAlarmNewController

@synthesize  editedObject,datePicker, tblView,daysarray,Addalarm,rowselection,temp, mins, secs, weekday, dayOfMonth, month, year,repeat,Alarmid;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
     daysarray =[[NSMutableArray alloc]initWithObjects:@"Every Monday",@"Every Tuesday",@"Every Wednesday",@"Every Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday",nil];
     temp = [[NSDictionary alloc] initWithObjectsAndKeys:daysarray,@"arrValue",nil];
     arrayValues = [[NSMutableArray alloc] initWithObjects:@"Every Monday",@"Every Tuesday",@"Ev开发者_开发百科ery Wednesday",@"Every Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday",nil];
     //self.daysarray = arrayValues;
     counter = 0;
     //[arrayValues release];
     [super viewDidLoad];

}


- (TAddAlarmController *)Addalarm {
    if (Addalarm == nil) {
        Addalarm = [[TAddAlarmController alloc] initWithStyle:UITableViewStyleGrouped];
    }
    return Addalarm;
}



#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
     return 1;
}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         // Return the number of rows in the section.
          return [arrayValues count];
 }


        // Customize the appearance of table view cells.
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

            static NSString *CellIdentifier = @"Cell";

            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }

            cell.textLabel.text = [daysarray objectAtIndex:indexPath.row];
            cell.accessoryType = ([indexPath isEqual:rowselection]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
            cell.detailTextLabel.textColor = [UIColor redColor];
            //return cell;

            // Configure the cell...

            return cell;
        }

        //this is localization code.
       -(void)selectmultiplerows
{
    newrepeat = [[NSString alloc]retain];

    if (!repeat)
    {

        BOOL mon = [self.arrayValues objectAtIndex:1] != nil;
        BOOL tue = [self.arrayValues objectAtIndex:2] != nil;
        BOOL wed = [self.arrayValues objectAtIndex:3] != nil;
        BOOL thu = [self.arrayValues objectAtIndex:4] != nil
        BOOL fri = [self.arrayValues objectAtIndex:5] != nil;
        BOOL sat =  [self.arrayValues objectAtIndex:6] != nil;
        BOOL sun =  [self.arrayValues objectAtIndex:0] != nil;  

        if (mon && tue && wed && thu && fri && sat && sun) {
            newrepeat = NSLocalizedString(@"everyday",@"Alarm description");
        }
        else if (mon && tue && wed && thu && fri) {
            newrepeat = NSLocalizedString(@"every week days",@"Alarm description");
        }
        else if (sat && sun){
            newrepeat = NSLocalizedString(@"every weekends",@"Alarm description");
        } 
        else if (mon || tue || wed || thu || fri || sat || sun) {
            //TODO
            //newrepeat =  NSLocalizedString(@"",@"Alarm description");
            if (mon) {
            //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"monday",@"Alarm description")];
                newrepeat = NSLocalizedString(@"monday",@"Alarm description");
            }
            if (tue) {
                newrepeat = NSLocalizedString(@"tuesday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"tuesday",@"Alarm description")];
            }
            if (wed) {
                newrepeat = NSLocalizedString(@"wednesday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"wednesday",@"Alarm description")];
            }
            if (thu) {
                newrepeat = NSLocalizedString(@"thursday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"thursday",@"Alarm description")];
            }
            if (fri) {
                newrepeat = NSLocalizedString(@"friday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"friday",@"Alarm description")];
            }
            if (sat) {
                newrepeat = NSLocalizedString(@"saturday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"saturday",@"Alarm description")];
            }
            if (sun) {
                newrepeat = NSLocalizedString(@"sunday",@"Alarm description");
                //newrepeat = [newrepeat stringByAppendingFormat:NSLocalizedString(@"sunday",@"Alarm description")];
            }
            //remove extra comma
            if ([newrepeat hasSuffix:@", "]) {
                newrepeat = [newrepeat stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
            }
        }

        else
        {
            newrepeat = NSLocalizedString(@"tuesday",@"Alarm description");
        }
    }



}


        #pragma mark -
        #pragma mark Table view delegate





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


    NSUInteger row = [indexPath row];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.detailTextLabel.textColor = [UIColor blackColor];


    if ([[tableView cellForRowAtIndexPath:indexPath ] accessoryType] == UITableViewCellAccessoryCheckmark)
    {
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
        [self.arrayValues replaceObjectAtIndex:row withObject:@"0"];
        cell = [tableView cellForRowAtIndexPath:indexPath];
        newrepeat = [NSString stringWithFormat:@"%@",cell];
    }
    else
    {
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
        [self.arrayValues replaceObjectAtIndex:row withObject:@"1 "];
        cell = [tableView cellForRowAtIndexPath:indexPath];

    }
    [self selectmultiplerows];



        }





        #pragma mark -
        #pragma mark Memory management

        - (void)didReceiveMemoryWarning {
            // Releases the view if it doesn't have a superview.
            [super didReceiveMemoryWarning];


        }

        - (void)viewDidUnload {

        }


        - (void)dealloc {
            [datePicker release];

            [super dealloc];
        }


        @end

And finally I am setting the newrepeat which is a global variable to the cellforrowatindexpath of h my first controller. But the problem is my condition does not gets satisfied. What may be the problem?

By changing the code of select multiplerows now it directly gets into the first if condition and and returns "every day " every time if I select 1 particular row also.


I have not checked all your logic conditions yet, but on first look, please use logical operators in if-clause instead of bitwise operators: in mon && tue & wed && thu && fri && sat && sun. Change tue & wed to tue && wed


If it does not retain (or does not exist), newRepeat is being set to auto-released objects returned by NSLocalizedString and stringWithFormat. You will have to retain these instances if you wish to use them later in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号