【问题标题】:UITableviewCell Image size changing on Click of cell?UITableviewCell 图像大小在单击单元格时发生变化?
【发布时间】:2015-01-09 15:08:05
【问题描述】:

我正在尝试像正常方式一样在 UITableViewCell 中显示名称和图像

cell.textLabel.text=@"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];

当我加载 ViewController 时,我可以看到所有图像都正确显示,但是当我尝试单击任何 UITableviewCell 时,图像大小变大(请查看示例截图)。

请帮我解决这个问题。

加载时间看起来像

当我点击第一个单元格时,单元格图像变大,如下图所示

请检查我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor=[UIColor clearColor];
        if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }
    cell.backgroundColor=[UIColor clearColor];
    if([userName count]>0){
    cell.textLabel.text=[userName objectAtIndex:indexPath.row];
        [cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:@"photo"] objectAtIndex:indexPath.row]]
                       placeholderImage:[UIImage imageNamed:@"avatar.png"]];
    }
    CGSize itemSize = CGSizeMake(40, 40);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    cell.imageView.layer.masksToBounds=YES;
    cell.imageView.layer.cornerRadius=9.0;


    return cell;
}

我的 Didselect 方法是这样的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *nib;

    if ([[MyDevice getMyDevice] isiPad])
    {
        nib = @"nibname_iPad";

    }
    else
    {
        nib = @"nibname";

    }


    ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
    chat.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:chat animated:YES];


}

【问题讨论】:

标签: ios objective-c uitableview uiimage


【解决方案1】:

尝试使用自定义单元格(在单元格的属性检查器中更改单元格的样式),它会更好,您可以按照自己的方式设计它。在 Story board 中创建 UILabel 和 UIImage(具有特定的宽度和高度),最后在 UILabel 和 UIIamge 的属性检查器中为它们设置一个特定的标签编号,这个编号对于它们来说必须是唯一的。并像这样访问它们:

在方法中:

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

插入:

    UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:@"theNameOfTheCell"];

    UILabel *label;
    label = (UILabel *)[customCell viewWithTag:1];
    label.text = @"Some text to label with tag number 1";
    label = (UILabel *)[customCell viewWithTag:2];
    label.text = @"Some text to label with tag number 2";

    UIImageView *imageCell;
    imageCell = (UIImageView *)[customCell viewWithTag:3];
    CALayer * l = [imageCell layer];
    UIImage *cellImage = [UIImage imageNamed:@"theImageYouWant.png"];
    imageCell.image = cellImage;

试试这个并告诉附加内容,这是完整的文档:Official Documentation Table View Cells

【讨论】:

  • 告诉我你有什么
  • 这是一个现有的大项目,无法从情节提要开始
【解决方案2】:

添加内容模式:

[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];

希望这会有所帮助... :)

【讨论】:

  • 还是一样...没有变化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
相关资源
最近更新 更多