Ios 当我收到通知时,在UITableView中添加新行的代码放在哪里?
我试图做的是将我从收到的通知中获得的信息放到UITableView中。我已经做到了,但是它只更新了第一行。也就是说,如果我向我的应用程序发送两个连续的通知,我将只看到第二个通知信息,因为它会覆盖第一个通知信息 如何添加另一行以包含通知数据? 我曾经配置推送通知Ios 当我收到通知时,在UITableView中添加新行的代码放在哪里?,ios,objective-c,iphone,uitableview,apple-push-notifications,Ios,Objective C,Iphone,Uitableview,Apple Push Notifications,我试图做的是将我从收到的通知中获得的信息放到UITableView中。我已经做到了,但是它只更新了第一行。也就是说,如果我向我的应用程序发送两个连续的通知,我将只看到第二个通知信息,因为它会覆盖第一个通知信息 如何添加另一行以包含通知数据? 我曾经配置推送通知 AppDelegate.m: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOpt
AppDelegate.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Pushbots sharedInstanceWithAppId:@"--My App ID--"];
[[Pushbots sharedInstance] receivedPush:launchOptions];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// This method will be called everytime you open the app
// Register the deviceToken on Pushbots
[[Pushbots sharedInstance] registerOnPushbots:deviceToken];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Notification Registration Error %@", [error userInfo]);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Handle notification when the user click it while app is running in background or foreground.
[[Pushbots sharedInstance] receivedPush:userInfo];
// you can get the required message as below
NSLog(@"UserInfo: %@", userInfo);
NSString *msg = [userInfo valueForKey:@"aps"];
NSString *alertMsg = [msg valueForKey:@"alert"];
NSLog(@"Push Notification:%@",alertMsg);
[[NSUserDefaults standardUserDefaults]setObject:alertMsg forKey:@"ReceivedNotifications"];
NSLog(@"Alert: %@", alertMsg);
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:nil]; //You can set object as nil or send the object you want to get from the ViewController
}
@implementation ViewController
{
NSMutableArray *notif;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.notifTableView.dataSource = self;
self.notifTableView.delegate = self;
NSUserDefaults *prevNotifTable = [NSUserDefaults standardUserDefaults];
NSMutableArray *prevNotif = [[prevNotifTable objectForKey:@"notifTableInfo"] mutableCopy];
notif = prevNotif;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"TestNotification" object:nil];
}
- (void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *checkAlert = [[NSString alloc] init];
checkAlert = [defaults stringForKey:@"ReceivedNotifications"];
NSLog(@"Alert Message: %@", checkAlert);
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
[self.notifTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [notif count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [notif objectAtIndex:indexPath.row];
return cell;
}
//For deleting a row
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Remove from NSMutableArray
[notif removeObjectAtIndex:indexPath.row];
//Remove from our table view
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
//[notif removeObjectAtIndex:indexPath.row];
//[tableView reloadData];
}
//For inserting a new row into the table
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.notifTableView setEditing:editing animated:animated];
//fill paths of insertion rows here
if (editing) {
[self.notifTableView insertRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
}
else {
[self.notifTableView deleteRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
ViewController.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Pushbots sharedInstanceWithAppId:@"--My App ID--"];
[[Pushbots sharedInstance] receivedPush:launchOptions];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// This method will be called everytime you open the app
// Register the deviceToken on Pushbots
[[Pushbots sharedInstance] registerOnPushbots:deviceToken];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Notification Registration Error %@", [error userInfo]);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//Handle notification when the user click it while app is running in background or foreground.
[[Pushbots sharedInstance] receivedPush:userInfo];
// you can get the required message as below
NSLog(@"UserInfo: %@", userInfo);
NSString *msg = [userInfo valueForKey:@"aps"];
NSString *alertMsg = [msg valueForKey:@"alert"];
NSLog(@"Push Notification:%@",alertMsg);
[[NSUserDefaults standardUserDefaults]setObject:alertMsg forKey:@"ReceivedNotifications"];
NSLog(@"Alert: %@", alertMsg);
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:nil]; //You can set object as nil or send the object you want to get from the ViewController
}
@implementation ViewController
{
NSMutableArray *notif;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.notifTableView.dataSource = self;
self.notifTableView.delegate = self;
NSUserDefaults *prevNotifTable = [NSUserDefaults standardUserDefaults];
NSMutableArray *prevNotif = [[prevNotifTable objectForKey:@"notifTableInfo"] mutableCopy];
notif = prevNotif;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"TestNotification" object:nil];
}
- (void) receiveNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *checkAlert = [[NSString alloc] init];
checkAlert = [defaults stringForKey:@"ReceivedNotifications"];
NSLog(@"Alert Message: %@", checkAlert);
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
[self.notifTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [notif count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [notif objectAtIndex:indexPath.row];
return cell;
}
//For deleting a row
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Remove from NSMutableArray
[notif removeObjectAtIndex:indexPath.row];
//Remove from our table view
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
//[notif removeObjectAtIndex:indexPath.row];
//[tableView reloadData];
}
//For inserting a new row into the table
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.notifTableView setEditing:editing animated:animated];
//fill paths of insertion rows here
if (editing) {
[self.notifTableView insertRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
}
else {
[self.notifTableView deleteRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
方法“-(无效)接收通知:(NSNotification*)通知”换行
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
到
我做了一个if-else语句来实现它。但是谢谢你!