2

嗨,我在 scheduleNotification 方法中获得正确的描述时遇到了一些问题,由于remediatearray 超出范围,我得到了sigbart。如何在nsmutablearray 中获取该特定索引的对象

我在 notify.alertbody 的 scheduleNotification 方法中得到了错误的描述,在 cellforrow 我得到了正确的描述

我无法获得正确的描述,因为我无法在 schedulenotification 方法中执行 objectatindex ..

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

    if (cell==nil) {
        cell=[[PPtableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.cellDelegate = self;
    }

    cell.notifyMe.on = NO;
    cell.remedyTextLabel.text=[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyTxtDic"];

    return cell;
 }

    else {
        return nil;
    }

}

- (UILocalNotification *)scheduleNotification :(int)remedyID {

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {

        UILocalNotification *notif = [[cls alloc] init];

        notif.timeZone = [NSTimeZone defaultTimeZone];

        NSLog(@"%d",remedyID);

        NSString *descriptionBody=[[remedyArray objectAtIndex:remedyID]objectForKey:@"RemedyTxtDic"];

        NSLog(@"%@",descriptionBody);

        notif.alertBody = [NSString stringWithString:descriptionBody];
4

2 回答 2

2
  • 小点,但-objectAtIndex:需要一个NSUInteger,而不是一个int

  • 我假设remedyArray一个实例变量(?)

我不认为你的问题出在你展示的方法上,除了参数-objectAtIndex:是错误的类型之外,它应该可以正常工作。你能显示正在调用的代码scheduleNotification:吗?

如果我是你,我不会使用对象的数组索引来定位它,你可能会遇到竞争条件和数组在通知安排时发生变化的情况。相反,您应该对数组进行线性搜索,或者如果这太慢,您应该保留它的哈希映射。

或者更好的是,您的 Remedy 对象应该是一个 NSManagedObject,然后您就可以免费获得所有的表处理和查找功能。

于 2013-01-24T17:14:41.613 回答
1

嗨,我能够解决它..这是我的代码

- (UILocalNotification *)scheduleNotification :(int)remedyID
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        UILocalNotification *notif = [[cls alloc] init];
        notif.timeZone = [NSTimeZone defaultTimeZone];

        NSString *descriptionBody;

        for (int i=0; i<remedyArray.count; i++)
        {
            int arrayid = [[[remedyArray objectAtIndex:i]objectForKey:@"RemedyID"]intValue];
            if (arrayid == remedyID)
            {
                descriptionBody=[[remedyArray objectAtIndex:i]objectForKey:@"RemedyTxtDic"];
                break;
            }

        }

        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:25];
        [components setMonth:1];
        [components setYear:2013];
        [components setMinute:45];
        [components setHour:11];

        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *date = [gregorian dateFromComponents:components];


        notif.alertBody = [NSString stringWithString:descriptionBody];
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;

        notif.fireDate = date;

        NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
                                                             forKey:@"kRemindMeNotificationDataKey"];
        notif.userInfo = userDict;

        [[UIApplication sharedApplication] scheduleLocalNotification:notif];

        return notif;
    }
    else
    {
        return nil;
    }

}
于 2013-01-25T06:32:42.897 回答