localNotification/uiTableView/删除行->错误“行数无效”
这是我的错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于行数更新前该部分中包含的行数 (2),加上或减去从该部分插入或删除的行数(0 插入,1 删除)以及加上或减去移入或移出该部分的行数(0 移动入,0 移出)。'
我知道这意味着什么,但我无法在代码中找到我的错误。我知道我只能使用 NSMutableArry 。不是正常的 NSArray。这是我认为的要点......
在我的 h.文件: NSMutableArray *notifArray, IBOutlet UITableView *myTable;
代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
代码:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [_notifArray objectAtIndex:indexPath.row];
<...>
代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
[notifArray removeObjectAtIndex:indexPath.row];
[self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable reloadData];
}
}
代码:
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = itemDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotification.alertBody = [eventText text];
// Set the action button
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotification.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
[self.myTable reloadData];
}
如果我将此行更改为 NSMutabelArray 则会出现错误,也。 “使用“NSArray*”类型的表达式初始化“NSMUtableArray”时不兼容的指针类型
---> NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
那么我能做什么,可以删除包含 localNotification 的行?
This is my ERROR:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
I know what that means but I cant find my mistake in code. I know that I have to use NSMutableArry, only. Not a normale NSArray.This is the Point I think...
In my h. File:
NSMutableArray *notifArray, IBOutlet UITableView *myTable;
CODE:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
CODE:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [_notifArray objectAtIndex:indexPath.row];
<...>
CODE:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
[notifArray removeObjectAtIndex:indexPath.row];
[self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable reloadData];
}
}
CODE:
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = itemDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotification.alertBody = [eventText text];
// Set the action button
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotification.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
[self.myTable reloadData];
}
If I change this line to a NSMutabelArray than I got an error, too. "Incompatible pointer types initializing "NSMUtableArray" with an expression of type "NSArray*"
---> NSArray *_notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
So what can I do, that it is possible to delete the row including a localNotification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常感谢!!!
我认为我的问题首先是一个错误的代码;-),然后我忘记了连续显示的通知有两件事!所以我必须首先删除我的 tableView 中的 theNotification 和第二次 theRow ;-)
这是我的代码 - 随意 ;-)
}
YEEAARRR 我很高兴 ;-) 无论如何,我在编码方面真的很新 ;-) - 所以如果有人有更好的方法请随时纠正我:-)
THANKS A LOT!!!
I think my Problem was at first a wrong code ;-) and at second I forgot that a notification displyed in a row are TWO THINGS! So I have to delete AT FIRST theNotification and second time theRow in my tableView ;-)
Here is my code - feel free ;-)
}
YEEAARRR Im so happy ;-) Im really new in coading anyway ;-) - So If anybody has a better way feel free to correct me :-)
关于初始化,您可以像这样创建可变数组:
并且您可能还需要保留它。
关于行删除,我想知道对 reloadData 的调用。我认为没有必要,因为上一行DeleteRows...导致表视图的更新,我什至想知道这是否可能是您的消息的原因。当然,它是在DeleteRows之后调用的,但我们没有真正的方法知道这一切是如何排序的,如果在DeleteRows完成之前重新加载查询numberOfRows,那么它可能会导致您的消息。
希望这有帮助。
Regarding the initilisation you can create the mutable array like this :
And you probably need to retain it as well.
Regarding the line deletion, I wonder about the call to reloadData. I don't think it is necessary, since the previous line DeleteRows... causes the update of the table view, and I even wonder if it could be the cause of your message. Of course it is called after DeleteRows but we have no real way of knowing how this is all sequenced, and if the reload queries numberOfRows before the DeleteRows is completed, then it can cause your message.
Hope this helps.