无法更新uitableview行

时间:2012-08-27 10:55:21

标签: iphone objective-c ios uitableview uialertview

我正在尝试更新UITableViewUIAlertView的所选didSelectRowAtIndexPath行。但我遇到了一个问题。每次我尝试更新任何一行时,它只会更新第一行而不管行的选择 我的代码是:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *selectedrow = nil;

if (searching) {
    slCell=indexPath.row;
    selectedrow = [copyListOfItems objectAtIndex:indexPath.row];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter the Number of Quantity" message:@""
                                               delegate:self cancelButtonTitle:nil  otherButtonTitles:@"OK", nil];

[alert addTextFieldWithValue:@"" label:@"Number of Item"];  
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter the Number of Quantity" message:@""
                                                   delegate:self cancelButtonTitle:nil  otherButtonTitles:@"OK", nil];

    [alert addTextFieldWithValue:@"" label:@"Number of Item"];  
    textfieldQty = [alert textFieldAtIndex:0];
    textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
    textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
    textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
    [alert show];
    [alert release];
        }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *b=nil;
if (buttonIndex == 0) {
    if (searching) {
        if([textfieldQty.text isEqualToString:b]) {
            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Enter plz" message:@""
                                                           delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK"];
            [alert2 show];
            [alert2 release];
        }


    NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty.text];
    DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];

    [d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];

    NSLog(@"tb%@",copyListOfQty);

    }
    else {

        NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty.text];
        DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
        [d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
        [self.tablevieww reloadData];
    }

}   
}

1 个答案:

答案 0 :(得分:1)

请按照以下步骤实现您的目标。

  1. UIAlertview方法中创建(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath时,将alerview的标记设置为indexpath的行。
  2. 在委托方法 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex中,您尝试访问alertView的标记并将该标记传递给方法调用[d->tableDataQt replaceObjectAtIndex:[alertView tag] withObject:(id)newqty];
  3. 在AppDelegate类中,您再创建一个名为-(void)replaceObjectAtIndex:(NSInteger>inIndex withObject:(id)inObject
  4. 的方法
  5. 在上面新创建的方法中,尝试使用方法调用UITableViewCell
  6. UITableView对象访问UITableViewCell *myCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:inIndex inSection:0];
  7. 这样,您将获得UITableViewCell的对象并更改其值并重新加载表。
  8. 我希望这会对你有所帮助。如果能解决您的问题,请不要忘记使用正确的标记。 :)

    感谢。