如何防止在UITableViewCell中覆盖值?

时间:2012-09-01 12:14:10

标签: iphone objective-c ios uitableview

当我选择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;
}

1 个答案:

答案 0 :(得分:0)

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

您应该将数组与SaveAlarmViewController分开存储。然后,您可以将一个项目添加到数组,并将整个数组传递给视图控制器,以将其显示为表格。