使用localizablestrings来满足特定条件并显示特定字符​​串

时间:2011-08-01 06:44:27

标签: iphone objective-c

我有一个应用程序,其中我有两个tableview控制器。在第一个tableview控制器中,当选择第二行时,新的tableview将加载7行。

通过使用NSMutableArray填充此tableview行中的值,其中静态地我已经在其中存储了7个值,然后我在no of rows方法中返回了数组。在这个控制器的didselect方法中,我调用一个调用localizable.strings文件的方法,并依赖于我的条件返回我的第一个tableview控制器的detailtext上的字符串。在第二个控制器中,我有每行7行的tableview,其值为“Every Monday”,“Every Tuesday”,“Every Wednesday”,“Every Thursday”,“Every Friday”,“Every Saturday”,“Every Sunday”。 / p>

  1. 当我点击特定行时,该特定行的值必须显示在第一个控制器的详细文本中。

  2. 当我点击前5行,即星期一到星期五时,必须在第一个控制器的详细文本上显示“每个工作日”的值。

  3. 当我点击最后两行时,即星期六&星期日必须在第一个控制器的详细文本上显示“每个周末”的值。

  4. 当我点击任意一行,即星期一,星期三,星期四时,必须在第一个控制器的详细文本上显示“每个星期一,星期三,星期四”的值。

  5. 这是我选择我的手机的第二个控制器代码:

    #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",@"Every 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
    

    最后我将newrepeat设置为我的第一个控制器的cellforrowatindexpath的全局变量。但问题是我的条件不满意。可能是什么问题?

    现在通过更改选择multiplerows的代码,它直接进入第一个if条件,并且如果我也选择1个特定行,则每次都返回“每天”。

2 个答案:

答案 0 :(得分:0)

我还没有检查过你的所有逻辑条件,但首先看一下,请使用if子句中的逻辑运算符而不是按位运算符:mon && tue & wed && thu && fri && sat && sun。将tue & wed更改为tue && wed

答案 1 :(得分:0)

如果它没有保留(或不存在),则newRepeat被设置为NSLocalizedString和stringWithFormat返回的自动释放对象。如果您希望稍后在代码中使用它们,则必须保留这些实例。