获取用户数据传递到表视图?

时间:2011-05-31 08:22:14

标签: ios iphone uitableview cocoa-touch view

我想开发一个类似的警报应用程序来提醒用户发送消息,我的问题是我想通过数组获取数据中的用户字段,如数字,消息和日期到表视图。

我这样做使用tabbar控制器,我的第一个视图是在所有数据中给用户字段并检查保存按钮。我的第二个视图是用于提醒用户的表视图。

现在我只能将数据保存在NSMutableArray中一次,我再次保存数据,旧数据替换。有谁知道怎么做?

我的代码在这里。

这是另一个视图的保存按钮。

我叫FirstView

- (IBAction)SaveAll:(id)sender{

    if(numText.text == nil || textView.text == nil || Selectime == nil){
        NSLog(@"error becos value null");

        UIAlertView *errorview = [[UIAlertView alloc]initWithTitle:@"Error Info" message:@"You nomber , message text or date no input or select.Pls fill in all. "delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [errorview show];
        [errorview release];
    }else{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (localNotif == nil)
        return;
    DataClass *obj=[DataClass getInstance]; 
    localNotif.fireDate = obj.showdate;

        NSString *getdate = [obj.showdate description];
        [defaults setObject:getdate forKey:@"date"];
    NSLog(@"localNotif = %@", localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone localTimeZone];

        obj.showdate = nil;


    NSString *getnum =numText.text;
    [defaults setObject:getnum forKey:@"keyToLookupString"];

    // Notification details
   // localNotif.alertBody = [eventText text];
        NSString *getmessage = self.textView.text;
        [defaults setObject:getmessage forKey:@"message"];

    localNotif.alertBody = self.textView.text;
    NSLog(@"alertBody message = %@",localNotif.alertBody);

    // Set the action button
    localNotif.alertAction = @"Send Now?";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;


    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
            [localNotif release];



    numText.text = @"";
    textView.text = @"";
    Selectime.text = @"";
    self.textView.placeholder = NSLocalizedString(@"Event Here....",);

        SaveDataView *savedata = [[SaveDataView alloc]initWithNibName:@"SaveView" bundle:nil];
        [savedata.tableview reloadData];
    }

}

在我的表视图控制器中,我调用SaveDataView。

SaveDataView.h

#import <UIKit/UIKit.h>


@interface SaveDataView : UIViewController <UITableViewDataSource,UITableViewDelegate>{
IBOutlet UITableView *tableview;
    NSMutableArray *MessageArray;
    NSMutableArray *NomberArray;
    NSMutableArray *DateArray;

}
@property (nonatomic, retain) IBOutlet UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *MessageArray;
@property (nonatomic,retain)NSMutableArray *NomberArray;
@property (nonatomic,retain)NSMutableArray *DateArray;

@end

SaveDataView.m

#import "SaveDataView.h"
#import "DataClass.h"
#import "SMSAppDelegate.h"
@implementation SaveDataView
@synthesize tableview;
@synthesize MessageArray,NomberArray,DateArray;

- (void) viewWillAppear {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *messageShow =[defaults stringForKey:@"message"];
    NSString *nomberShow = [defaults stringForKey:@"keyToLookupString"];
    NSString *dateShow = [defaults stringForKey:@"date"];

    [MessageArray addObject:messageShow];
    NSLog(@"MessageArray = %@",MessageArray);
    [NomberArray addObject:nomberShow];
    NSLog(@"NomberArray = %@",NomberArray);
    [DateArray addObject:dateShow];
    NSLog(@"DateArray = %@",DateArray);

    [self.tableview reloadData];
    NSLog(@"checking.... ");
    //[super viewWillAppear:animated];
}
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    NSLog(@"table view viewDidLoad");


    MessageArray = [[NSMutableArray alloc] init];
    NomberArray = [[NSMutableArray alloc] init];
    DateArray = [[NSMutableArray alloc] init];
    //  [MessageArray insertObject:messageShow atIndex:[MessageArray count]];
    [self viewWillAppear];

    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

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

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#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 [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
    NSLog(@"in nsinteger tableview");
    return [self.MessageArray count];
    return [self.NomberArray count];
    return [self.DateArray count];

}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"in uitableviewcell...");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    // if (cell == nil) {
    //  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    //} 
    // Configure the cell...

    //NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
    //  NSString *enNote = [[NSString alloc] initWithString:string];

    NSString *showmessage = [MessageArray objectAtIndex:indexPath.row];
    NSString *shownomber = [NomberArray objectAtIndex:indexPath.row];
    NSString *showdate = [DateArray objectAtIndex:indexPath.row];

    [cell.textLabel setText:showmessage];
    NSLog(@"table view settext = %@",showmessage);
    cell.textLabel.textColor = [UIColor blackColor];



    NSString *abc = [NSString stringWithFormat:@"%@ \n %@", shownomber,showdate];
//NSString *abc = [NSString stringWithFormat:@"%@ \n %@", nomber,[notif.fireDate description]];
    [cell.detailTextLabel  setText:abc];
    cell.detailTextLabel.numberOfLines = 2;
    NSLog(@"table view detailTextLabel = %@,%@",shownomber,showdate);
    cell.detailTextLabel.textColor = [UIColor redColor];
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    return cell;

    [self.tableview reloadData];
}




@end

1 个答案:

答案 0 :(得分:0)

将viewDidLoad替换为viewWillappear ...在加载视图时只调用一次viewDidLoad,因此您无法替换该数组。每当视图出现在屏幕上时都会调用viewWillappear ...

编辑:看到评论后。 保持viewdidload不变。将这些代码添加到您的应用程序中。

  -(void)viewwillappear
    {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *messageShow =[defaults stringForKey:@"message"];
    NSString *nomberShow = [defaults stringForKey:@"keyToLookupString"];
    NSString *dateShow = [defaults stringForKey:@"date"];

     [MessageArray addObject:messageShow];
    NSLog(@"MessageArray = %@",MessageArray);
    [NomberArray addObject:nomberShow];
    NSLog(@"NomberArray = %@",NomberArray);
    [DateArray addObject:dateShow];
     NSLog(@"DateArray = %@",DateArray);

    }