简体   繁体   中英

reloadData doen't draw cells in tableview

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

NSString *show=[NSString stringWithFormat:@"%@",[res objectAtIndex:18]];
float f=[show floatValue];
show1=[NSString stringWithFormat:@"%.2f %%",f];
[show1  retain];
[self.tableView reloadData];
}


- (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];
        cell.detailTextLabel.text=show1;
    }
    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;
}   

This is my code as you can see i reload data in viewDidAppear show1 is my data that needs to put in the tableView's cell in detailLabel.text. Now don't look at my odd cell part cos its not useable atm. I got only 1 cell and its even cell. Now my problem is when i delete the [self.tableView reloadData]; line it works good not updating but it shows the cell.When i put it i can't see a cell in tableView its all empty any idea why? where am i doing wrong?

edit1:

  cell.backgroundView = bg;
    cell.textLabel.backgroundColor = bg.backgroundColor;
    [bg release];
    cell.detailTextLabel.backgroundColor=[UIColor clearColor];
}
cell.detailTextLabel.text=show1;
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

okey i change it and get it out of if but still i can't see any table cell its all empty just the navigation bar name and empty cell . Why its empty anyidea?

The String value Show1 is set only during initialing the cell.

cell.detailTextLabel.text=show1;

Put the above line below this line and check.

cell.textLabel.text = [array objectAtIndex:indexPath.row];

The code "cell.detailTextLabel.text=show1;" is inside the if condition of if( cell==nil) When you are reloading the cell if the cell is already available you will not go in the code block and you changed value of show1 won't be displayed.

The code
cell = [tableView dequeueReusableCellWithIdentifier:@"EvenCell"];

Checks if their is a cell that it can re-use. If it finds that then it would return the same

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM