UIImage 在标签上加载

Posted

技术标签:

【中文标题】UIImage 在标签上加载【英文标题】:UIImage loads over Labels 【发布时间】:2014-04-30 14:48:16 【问题描述】:

我有一个 TableViewController,它使用 JSON 从我的主机上的服务器加载内容。 一切正常,但是 UIImage 应该全部加载在我的标签之上。 为什么会发生这种情况,如何更改对象的顺序? 在我的故事板中,一切都是正确的,只有在加载内容时才会发生这种变化。

这是代码。

- (void)viewDidLoad

    [super viewDidLoad];

    // CHECK INTERNET CONNECTION
    if ([self IsConnected]) 
        [self retrieveData];
    
    else 
        UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:@"MSG ERRO" message:@"Conecte a Internet" delegate:nil cancelButtonTitle:@"fechar" otherButtonTitles:nil, nil];

        [alerta show];
    



#pragma mark - PULL TO REFRESH

- (void)refresh

    if ([self IsConnected]) 
        [self retrieveData];
        [self.refreshControl endRefreshing];
    
    else 
        UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:@"MSG ERRO" message:@"Conecte a Internet" delegate:nil cancelButtonTitle:@"fechar" otherButtonTitles:nil, nil];
        [alerta show];

        [self.refreshControl endRefreshing];
    


#pragma mark - CHECK INTERNET CONNECTION METHOD

- (BOOL)IsConnected

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];

    return !(networkStatus == NotReachable);


#pragma mark - TABLE VIEW

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return programacaoArray.count;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *identifier = @"TableCell";
    TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) 
        cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    

    cell.backgroundColor = [UIColor clearColor];

    // CONFIGURE THE CELL
    Programacao *programacaoObject;
    programacaoObject = [programacaoArray objectAtIndex:indexPath.row];

    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:programacaoObject.programacaoImagem]]];
    cell.nomeLabel.text = programacaoObject.programacaoNome;
    cell.dataLabel.text = programacaoObject.programacaoData;
    cell.localLabel.text = programacaoObject.programacaoLocal;

    return cell;



#pragma mark - LOADS THE DATA FROM SERVER

- (void) retrieveData


    NSURL *url = [NSURL URLWithString:getDataURL];
    NSData *data = [NSData dataWithContentsOfURL:url];

    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    // SETUP programacaoArray
    programacaoArray = [[NSMutableArray alloc] init];

    // Loop Array
    for (int i = 0; i < jsonArray.count; i++) 
        // Cria o PROGRAMACAO Objeto
        NSString *pID         = [[jsonArray objectAtIndex:i] objectForKey:@"id"];
        NSString *pNome       = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoNome"];
        NSString *pImagem     = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoImagem"];
        NSString *pDescricao  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoDescricao"];
        NSString *pData       = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoData"];
        NSString *pLocal      = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoLocal"];
        NSString *pPrecos     = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoPrecos"];

        // ADD THE CONTENT
        [programacaoArray addObject:[[Programacao alloc] initWithNome:pNome andImagem:pImagem andDescricao:pDescricao andData:pData andLocal:pLocal andPrecos:pPrecos andID:pID]];
    

// RELOAD TABLE VIEW
[self.tableView reloadData];

这是我的屏幕。

【问题讨论】:

你在TableCell类中做布局吗? 不,我只是在 .h 文件中创建我的 IBOutlets。 @property (strong, nonatomic) IBOutlet UILabel *nomeLabel; @property (strong, nonatomic) IBOutlet UILabel *dataLabel; @property (strong, nonatomic) IBOutlet UILabel *localLabel; @property (strong, nonatomic) IBOutlet UIImageView *atracaoImagem; 我的 .m 文件中没有任何内容。 【参考方案1】:

您确定programacaoObject.programacaoNomeprogramacaoObject.programacaoDataprogramacaoObject.programacaoLocal 不为空吗?

【讨论】:

是的,因为如果我评论加载图像的行。标签正常显示

以上是关于UIImage 在标签上加载的主要内容,如果未能解决你的问题,请参考以下文章

在标签栏顶部始终显示 uiimage

如果设置为 .scaleAspectFill,UIImage 会与标签重叠

UIimage 未在 tableview 上加载

UIImage加载方式

UIImage加载图片的区别

根据标签设置 UIImage