tableview自动随意更新

时间:2011-08-03 10:27:01

标签: iphone objective-c uitableview

@interface NavigationController : UITableViewController 

我的tableview是以编程方式创建的,因此没有xib文件。在用户单击标签栏时,我会更新表格。这就是我把它放在viewDidAppear中的原因。但是这个表每次都不会自动更新,只是随机更新。你可以看到表中写的show值.nslog写的是正确的值,但表没有显示正确的值,任何想法为什么? ?

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSString *country=[[NSUserDefaults standardUserDefaults]objectForKey:@"namecountry"];
//url's
NSURL *url = [NSURL URLWithString:@"someurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:country forKey:@"c_name"];
[request setRequestMethod:@"POST"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startSynchronous];

NSString *response = [request responseString];
NSLog(@"%@",response);
res=[[response componentsSeparatedByString:@","] retain];
NSLog(@"%@",[res objectAtIndex:18]);
show=[[NSString stringWithFormat:@"%@",[res objectAtIndex:18]] retain];
float f=[show floatValue];
show=[NSString stringWithFormat:@"%.2f %%",f];
}

编辑::

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (indexPath.row % 2 == 0) {
    // EVEN
    cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"EvenCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
        UIColor *colour = [[UIColor alloc] initWithRed: (208.0/255.f) green: (231.0/255.f) 
                                                  blue: (241.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour; 
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
        cell.detailTextLabel.backgroundColor=[UIColor clearColor];
        show=[[NSString stringWithFormat:@"%@",[res objectAtIndex:18]] retain];
        float f=[show floatValue];
        show=[NSString stringWithFormat:@"%.2f %%",f];
        cell.detailTextLabel.text=show;
    }
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

} else {
    // ODD
    cell = [tableView dequeueReusableCellWithIdentifier:@"OddCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"OddCell"] autorelease];
        UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

        UIColor *colour = [[UIColor alloc] initWithRed: (143.0/255.f) green: (169.0/255.f) 
                                                  blue: (180.0/255.f) alpha: 1.0];
        bg.backgroundColor = colour;
        cell.backgroundView = bg;
        cell.textLabel.backgroundColor = bg.backgroundColor;
        [bg release];
    }
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


} 
return cell;
}   

1 个答案:

答案 0 :(得分:1)

要在tableview中显示新数据,您需要调用

[self.tableView reloadData];

这指示tableview调用所有数据源方法并获取新数据。如果不进行此调用,它将继续使用缓存数据。