UITableViewCell imageView não aparece
-
13-12-2019 - |
Pergunta
Então eu tive o seguinte código:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"FriendsCell";
FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
[cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
[cell.textLabel setText:fd.name_];
return cell;
}
Porém, não estou vendo o imageView na célula.O que estou fazendo de errado?
Solução
1) Definir imagem com URL não é um método iOS.É algo personalizado e esse pode ser o problema.Mas até que você publique, não pode ajudar.
2) Acho que cell.imageView irá ignorar "setFrame".Não consigo fazer isso funcionar em nenhuma das células da minha tabela usando uma imagem.Sempre parece que o padrão é a largura da imagem.
3) Normalmente você define a imagem usando cell.imageView.image = your_Image.O ImageView é READONLY e provavelmente o quadro ImageView é privado.
4) Acho que você precisará criar sua própria célula personalizada.
Outras dicas
Também tive o mesmo problema com SDImageCache.Minha solução é colocar uma imagem de espaço reservado com o tamanho de quadro desejado.
UIImage *placeholder = [UIImage imageNamed:@"some_image.png"];
[cell.imageView setImage:placeholder];
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
O uso de um método com espaço reservado resolverá o problema.
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]placeholderImage:[UIImage imageNamed:@"placeholder"]];
Você precisa return cell
no final do método
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"FriendsCell";
FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
[cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
[cell.textLabel setText:fd.name_];
return cell;
}