reloadData不会在tableview中绘制单元格 [英] reloadData doen't draw cells in tableview

查看:47
本文介绍了reloadData不会在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;
}   

这是我的代码,您可以看到我在viewDidAppear中重新加载数据show1是我的数据,需要在detailLabel.text中放入tableView的单元格中.现在,不要看我的奇数单元部分,因为它不可用的atm.我只有1个单元格和偶数个单元格.现在我的问题是当我删除[self.tableView reloadData]时;行它的工作原理是不更新,但不显示它,但是它显示了单元格.当我放它时,我在tableView中看不到一个单元格,它的所有内容都为空,为什么?我在哪里做错了?

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?

推荐答案

代码"cell.detailTextLabel.text = show1;"在if(cell == nil)的if条件内如果单元已经可用,则在重新加载单元时,您将不会进入代码块,并且不会显示您更改的show1值.

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.

代码
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

这篇关于reloadData不会在tableview中绘制单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆