0

当我选择 UISegmentControl 的按钮时,我在另一个视图控制器上传递了两个 UITextField 值。这些值存储在 NSMutableDictionary 中。我将 NSMutableDictionary 存储在 NSMutableArray 中。

我的问题是,当我第二次传递值然后从 NSMutableDictionary 中删除前一个值时,只显示最后传递的值。

这是我写的代码:

-(IBAction)actionOfSaveandSettingSegment:(id)sender
{
    switch ([self.SaveSgCon selectedSegmentIndex])
    {
        case 0:
        {
                UILocalNotification *localNotiStr = [[[UILocalNotification alloc] init] autorelease];
                localNotiStr.fireDate = self.DatePicker.date;
                localNotiStr.alertBody = @"Please Get Up With New Dream...!!!";
                localNotiStr.alertAction=@"View";
                localNotiStr.soundName = UILocalNotificationDefaultSoundName;


               [UIApplication sharedApplication].applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotiStr];


                SaveAlarmViewController *addView=[[SaveAlarmViewController alloc] init];
                addView.alarmNm=self.alaramName.text;
                addView.alarmTime=self.alaramTime.text;
                addView.notificationsArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
                [self.navigationController pushViewController:addView animated:YES];
                [addView release];

           break;
        }
        default:
            NSLog(@"InValid Selection..!!!");
    }
}

SaveAlarmViewController.h

#import <UIKit/UIKit.h>

@interface SaveAlarmViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSString *alarmNm;
    NSString *alarmTime;

    IBOutlet UITableView *tblAlarmList;
    NSMutableDictionary *AlarmDataDictonry;

    NSArray *notificationsArray;
    NSMutableArray *alramArr;

}

@property(nonatomic,retain)NSMutableArray *alramArr;
@property(nonatomic,retain)NSArray *notificationsArray;
@property(nonatomic,retain)NSString *alarmNm;
@property(nonatomic,retain)NSString *alarmTime;
@property(nonatomic,retain)UITableView *tblAlarmList;
@property(nonatomic,retain)NSMutableDictionary *AlarmDataDictonry;

@end

SaveAlarmViewController.m

- (void)viewDidLoad
{
    self.AlarmDataDictonry=[[NSMutableDictionary alloc] init];
    self.alramArr=[[NSMutableArray alloc] init];
    [self.AlarmDataDictonry setObject:self.alarmNm forKey:@"ALARM_NAME"];
    [self.AlarmDataDictonry setObject:self.alarmTime forKey:@"ALARM_TIME"];    
    [self.alramArr addObject:self.AlarmDataDictonry];

   [super viewDidLoad];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.alramArr count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            NSString *strRowNo=[NSString stringWithFormat:@"%d )",indexPath.row+1];

            UILabel * rowCount = [[UILabel alloc] initWithFrame:CGRectMake(07,20,25,25)];
            rowCount.text = strRowNo;

            [cell.contentView addSubview:rowCount];
    }


    UILabel * firstNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,20,100,25)];
    firstNameLabel.text = [[self.alramArr objectAtIndex:indexPath.row] objectForKey:@"ALARM_NAME"];

    UILabel * lastNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,70, 100, 25)];
    lastNameLabel.text = [[self.alramArr objectAtIndex:indexPath.row] objectForKey:@"ALARM_TIME"];

      [cell.contentView addSubview:firstNameLabel];
    [cell.contentView addSubview:lastNameLabel];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
     return cell;
}
4

1 回答 1

0

每次SaveAlarmViewController加载时,它都会创建一个self.alramArr只有一个字典的新数组。因此,表格视图将始终只显示一行。

您应该将数组与SaveAlarmViewController. 然后您可以向数组中添加一个项目并将整个数组传递给视图控制器以将其显示为表格。

于 2012-09-01T14:51:30.157 回答