UILabel显示错误

时间:2014-06-17 17:14:00

标签: ios uitableview uilabel

我使用tableview从我的大学数据库加载数据,该表正常加载tablecell ...但是当我向下滚动表时,该学科的名称进展顺利,但成绩正在显示一个上面的

Screenshot

为什么会这样?

#import "NFMainViewController.h"
#import "NFData.h"

@interface NFMainViewController ()

@end

@implementation NFMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        data = [[NFData getData] objectForKey:@"data"];
        cursoData = nil;
        cursosView = [[UIViewController alloc] init];
        [cursosView setTitle:@"Cursos"];
        cursosTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [cursosTable setDelegate:self];
        [cursosTable setDataSource:self];
        [cursosView.view addSubview:cursosTable];
        [self pushViewController:cursosView animated:NO];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == cursosTable) {
        return [data count];
    } else {
        return [cursoData count];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *ident = @"headerIdent";
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ident];
    if (view == nil) {
        view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:ident];
    }
    if (tableView == cursosTable) {
        view.textLabel.text = [[data objectAtIndex:section] objectForKey:@"unidade"];
    } else {
        NSDictionary *temp = [cursoData objectAtIndex:section];
        view.textLabel.text = [NSString stringWithFormat:@"%@º/%@", [temp objectForKey:@"semestre"], [temp objectForKey:@"ano"]];
    }
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == cursosTable) {
        return [[[data objectAtIndex:section] objectForKey:@"cursos"] count];
    } else {
        return [[[cursoData objectAtIndex:section] objectForKey:@"disciplinas"] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"cellIdent";
    UITableViewCell *view = [tableView dequeueReusableCellWithIdentifier:ident];
    if (view == nil) {
        view = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
    }
    if (tableView == cursosTable) {
        view.textLabel.text = [[[[data objectAtIndex:indexPath.section] objectForKey:@"cursos"] objectAtIndex:indexPath.row] objectForKey:@"curso"];
    } else {

        UIFont *font = [UIFont fontWithName:@"Arial" size:10.0f];

        view.textLabel.font = font;
        view.selectionStyle = UITableViewCellSelectionStyleNone;

        NSDictionary *temp = [[[cursoData objectAtIndex:indexPath.section] objectForKey:@"disciplinas"] objectAtIndex:indexPath.row];
        view.textLabel.text = [temp objectForKey:@"disciplina"];

        CGRect notaRect = view.bounds;
        notaRect.origin.x = notaRect.size.width - 70.0f;
        notaRect.size.width = 50.0f;
        UILabel *nota = [[UILabel alloc] initWithFrame:notaRect];
        nota.textAlignment = NSTextAlignmentRight;
        nota.font = font;
        nota.text = [temp objectForKey:@"nota"];
        [view addSubview:nota];

        CGRect labelRect = view.textLabel.frame;
        labelRect.size.height -= 60;
        view.textLabel.frame = labelRect;

        CGRect progRect = view.bounds;
        progRect.origin.x += 6.0f;
        progRect.size.width -= 12.0f;
        progRect.origin.y += progRect.size.height - 6.0f;
        progRect.size.height = 5.0f;
        UIProgressView *prog = [[UIProgressView alloc] initWithFrame:progRect];
        int faltas = [[temp objectForKey:@"faltas"] intValue];
        int maximo = [[temp objectForKey:@"maximo"] intValue];
        float value = 1.0f * faltas / maximo;
        if (value > 1.0f) {
            prog.progressTintColor = [UIColor blackColor];
        } else if (value == 1.0f) {
            prog.progressTintColor = [UIColor redColor];
        } else if (value >= 0.7f) {
            prog.progressTintColor = [UIColor yellowColor];
        }
        [prog setProgress:value];
        [view addSubview:prog];
    }
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == cursosTable) {
        cursoData = [[[[data objectAtIndex:indexPath.section] objectForKey:@"cursos"] objectAtIndex:indexPath.row] objectForKey:@"epocas"];
        notasView = [[UIViewController alloc] init];
        [notasView setTitle:@"Disciplinas"];
        notasTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [notasTable setDelegate:self];
        [notasTable setDataSource:self];
        [notasView.view addSubview:notasTable];
        [self pushViewController:notasView animated:YES];
    }
}

@end

1 个答案:

答案 0 :(得分:3)

每次UILabel出列时,都会创建你的nota UITableViewCell。所以tableview第一次加载一切都很好。然后,当您开始滚动时,您的代码会重复使用已创建nota标签的单元格,但您会在其上添加另一个标签。您需要重复使用先前创建的标签。

最好的方法是创建一个带有nota属性的UITableViewCell子类。