按钮clicked-iphone在表格视图中更改数据

时间:2011-12-12 07:25:06

标签: iphone ios uitableview

我是iPhone应用程序开发的新手。 在我的应用程序中,当我点击搜索按钮时,我想在表格视图中显示记录。 我使用数组来更改表视图中的记录,但它不起作用..请帮助

viewController.m

-(IBAction) printButtonPressed : (id) sender
{
    mylabel.text =keyfileld.text;
    UITableView *tableView ;
    NSIndexPath *indexPath;
    mylabel.text =keyfileld.text;

    NSString *post =[NSString stringWithFormat:@"username=%@",keyfileld.text];
    NSString *hostStr = @"http://demo.com/search_iphone.php?";
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

    [states removeObjectForKey:@"1"];   
    [states removeObjectForKey:@"2"]; 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];        
    }

    cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if(serverOutput)
    {
        NSArray *listItems = [serverOutput componentsSeparatedByString:@"#"];
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        mylabel.text = serverOutput;
        NSMutableArray *stringArray = [[NSMutableArray alloc] init];
        int x=1;
        for (int i=0;i<listItems.count; i++) 
        { 
            NSString *myString = [NSString stringWithFormat:@"%d",x];
            NSArray *listItemsid = [[listItems objectAtIndex:i] componentsSeparatedByString:@","];
            NSLog(@"place name at index %d: %@\n", i, [listItemsid objectAtIndex:1]); 
            [states setObject:[listItemsid objectAtIndex:1] forKey:myString];
            x++;
            [stringArray insertObject:[NSMutableArray arrayWithObjects:str,nil] atIndex:i];         
        } 

        datasource = [states allKeys];

    } else {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect"
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];

        // [alertsuccess release];
    }
}

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    id aKey = [datasource objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [states objectForKey:aKey];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   

    return cell;
}

1 个答案:

答案 0 :(得分:2)

尝试调用[tableView reloadData];将新数据加载到数组后...