更改表视图中的数据,点击按钮-iphone



我是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;
}

尝试调用[tableView reloadData];

最新更新