1

我正在使用 aUITableView来显示数据,并通过使用自定义按钮和删除功能,我试图删除选定的行。但是我想在该函数UITableView为空时放置一个警报视图,并通过使用里面的按钮,UIAlertView我试图根据条件导航到主页和上一页。但是它在UITableView变空后崩溃了,我按下删除按钮并显示“程序接收信号:”SIGABRT“。

我的代码如下所示:

    - (IBAction)DeleteButtonAction:(id)sender
    {   
        DMSAppDelegate *d = (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];

        NSLog(@"Message From Custom Cell Received");

        if(d->newtableData.count != 0)
        {

            NSIndexPath *indexPath = [self.tablevieww2 indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
            NSUInteger row = [indexPath row];
            [d->newtableDataQt removeObjectAtIndex:row];
            NSLog(@"data removed");
            [self.tablevieww2 reloadData];
        }   
        else
        {       
            UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"hello" message:@"Warning!!: Table is empty" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"",@"No",nil];
            textfieldQty1 = [alertview textFieldAtIndex:0];
            textfieldQty1.keyboardType = UIKeyboardTypeNumberPad;
            textfieldQty1.keyboardAppearance = UIKeyboardAppearanceAlert;
            textfieldQty1.autocorrectionType = UITextAutocorrectionTypeNo;
            [alertview show];
            [alertview release];    
        }

    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
        if (buttonIndex == 0) 
        {
            DMSViewController *bt=[[DMSViewController alloc]initWithNibName:nil bundle:nil];
            bt.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
            [self presentModalViewController:bt animated:YES];
        }
        else if (buttonIndex == 1)
        {
            NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty1.text];
            DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
            [d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
            NSLog(@"tb%@",d->newtableDataQt);
            [self.tablevieww2 reloadData];  
            int total1=0;

            for ( int i=0 ; i < [d->newtableDataQt count];++i )
            {           
                NSString *string = [d->newtableDataQt objectAtIndex:i];

                NSLog(@"string%@",string);

                if ([string isEqualToString:@"0"])
                {

                }
                else
                {
                    NSLog(@"newArray%@",d->newtableDataPrice);
                    NSString *strP=[d->tableDataPrice objectAtIndex:i];
                    NSInteger sp=[strP integerValue];
                    NSInteger st=[string integerValue];
                    total1=total1+st*sp;
                    NSLog(@"total1%d",total1);
                }
            }

            NSString *newtotal1=[NSString stringWithFormat:@"%d",total1];
            DMSAppDelegate *d2 = (DMSAppDelegate   *) [[UIApplication sharedApplication] delegate];
            d2->totalD = [[NSString alloc] initWithString:newtotal1];
        }   
    }

    Please give me some solution. I am trying really hard from yesterday but not getting any success.
    Thanks in advance.
    @
4

1 回答 1

1

您需要检查两件事:-

首先:-if(d->newtableData.count != 0) 是条件,您没有从newtableData您的项目中删除项目,newtableDataQt这就是您的 else 方法没有被调用的原因。因为newtableData永远不会有count = 0。

第二件事; - 如果您的表为空意味着不newtableDataQt包含任何值,它将为空。现在,当您单击删除按钮时,会出现警报视图,之后如果您单击索引 1 处的任何按钮,然后在您的您编写的代码:-

[d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];

所以newtableDataQt之前已经是空的,现在你正在使用它。这可能是崩溃的原因。

尝试

if( [newtableDataQt count] >slCell1)
{    [d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
}

我希望它可以帮助你。

于 2012-08-30T06:43:53.867 回答