簡體   English   中英

以NSException 4類型的未捕獲異常終止

[英]terminating with uncaught exception of type NSException 4

我正在制作一個使用tableview的應用程序,並在每個表格單元格中使用復選框。 現在我被困在這個地方,當選中復選框時,我想獲取該表格單元的值。 我在表格單元格中顯示消息ID,我想要獲取選中其復選框的該單元格的messages_Id 意味着如果我選擇1個復選框,則其消息ID存儲在NSString ;如果我選擇2個復選框,則將這些復選框的2條消息存儲在字符串中,這是如何實現的。 我已經制作了要在其中存儲復選框消息ID的數組,但是我遇到了以下異常。 以下是我的復選框動作示例代碼

-(IBAction)checkButton:(UIButton *)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.activitiesTableView_];
    NSIndexPath *indexPath = [self.activitiesTableView_ indexPathForRowAtPoint:buttonPosition];

    if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])               {
        [self.checkimageArray removeObject:[self.lblArray objectAtIndex:indexPath.row]];
        [self.checkboxmessageArray removeObject:[[self.inboxmessagesarray objectAtIndex:sender.tag]objectForKey:@"messageid"]];//here i am removing whien chekbox is unchecked
    else {
        [self.checkimageArray addObject:[self.lblArray objectAtIndex:indexPath.row]];
        [self.checkboxmessageArray addObject:[[self.inboxmessagesarray objectAtIndex:sender.tag]objectForKey:@"messageid"]];// i am getting exception in this line
    }
}

為什么我將這種異常困在其中很多天的原因是什么?

在cellForRowAtIndexPath方法中,將按鈕標簽值分配給indexPath值,例如

checkbox.tag=indexPath.row

從中獲取價值

-(IBAction)checkButton:(UIButton *)sender
{

    [self.checkimageArray objectAtIndex:sender.tag];
    ///additional code

}

就像在ViewDidLoad創建一個常見的NSMutableArray

self.checkboxmessageArray =[NSMutableArray New];



-(IBAction)checkButton:(UIButton *)sender
    {
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tblvie];
        NSIndexPath *indexPath = [self.tblvie indexPathForRowAtPoint:buttonPosition];

        if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]]) {
            [self.checkimageArray removeObject:[self.lblArray objectAtIndex:indexPath.row]];
            [self.checkboxmessageArray removeObject:[[self.inboxmessagesarray objectAtIndex:sender.tag]objectForKey:@"messageid"]];


        }


        else {
            [self.checkimageArray addObject:[self.lblArray objectAtIndex:indexPath.row]];

            messageid.text=[[self.inboxmessagesarray objectAtIndex:sender.tag]objectForKey:@"messageid"];
            [self.checkboxmessageArray addObject:[[self.inboxmessagesarray objectAtIndex:sender.tag]objectForKey:@"messageid"]];//assume that this is your message ID array,  here u convert the string



                            }
                            [self.tblvie reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];

}

如果您想將messageID發送到服務器,請按照此步驟自定義

 NSString *finalIDStr;

if (self.checkboxmessageArray .count==0)
{
    finalIDStr=@"";
}
else
{


    NSData *data=[NSJSONSerialization dataWithJSONObject: self.checkboxmessageArray options:kNilOptions error:nil];
   finalIDStr=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"the final ID Str==%@",finalIDStr);

}

最終將此yourmessageidStr字符串傳遞給服務器

Console輸出就像

  the final ID Str=["658681","655917","655904"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM