iphone - UITableViewCell 带图片自定义尺寸

标签 iphone ios xcode uiimageview

我正在通过简单的方式将图像加载到 UITableView 中

cell.imageView.image = [UIImage imageNamed:@"prem.jpg"]; 

但是,我在有限的时间里使用 xcode 学到的东西通常不是那么容易!我想让那里的图像占据大约 280 宽和 150 高的图像的全尺寸。通过谷歌搜索,我可能必须构建一个自定义单元格并将图像放在那里,然后加载该自定义单元格?我以前没有做过类似的事情,只加载一张图片似乎有点烦人。有其他选择吗?

在上下文中,我在 tableview 顶部有 3 个部分是图像,另外两个是从 XML 文件加载的数组。如果我做一个自定义单元格,我必须做一个单独的 xml 调用以在它通过之前获取图像 url,所以我真的很想尽可能避免这种情况?

真的在寻找您可以提供的任何指示。非常感谢

汤姆

根据要求编辑:cellforrowindexpath:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.section == 0)


        cell.imageView.image = [UIImage imageNamed:@"prem.jpg"];




    if(indexPath.section == 1)

        cell.textLabel.text = [arryTableData objectAtIndex:indexPath.row];


    else if(indexPath.section == 2)
        cell.textLabel.text = [arryTableActions objectAtIndex:indexPath.row];

    return cell;


}

最佳答案

无需创建自定义单元格子类。我假设您在第 0 节中有一个单元格,在第 1 节和第 2 节中有可变数量的单元格。

您应该为第 0 节中的单元格使用不同的单元格重用标识符,因为它包含 ImageView 而其他单元格不包含 - 当单元格被回收时,这将显示在您后面的部分中。

我会尝试这样的事情:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{      
    static NSString *CellIdentifierText = @"TextCell";
    static NSString *CellIdentifierImage = @"ImageCell";

    if (indexPath.section == 0)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierImage];
        // I am assuming this image never changes and there is one row in this section! If it does change, use viewWithTag to get hold of the image view.
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierImage] autorelease];
                   UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,250,180)];              
                   myImageView.tag = 1;
             myImageView.image = [UIImage imageNamed:@"prem.jpg"];
            [cell addSubview:myImageView];
                       [myImageView release];
        }
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierText];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierText] autorelease];
        }      
        if(indexPath.section == 1)          
            cell.textLabel.text = [arryTableData objectAtIndex:indexPath.row];       
        else if(indexPath.section == 2)         
            cell.textLabel.text = [arryTableActions objectAtIndex:indexPath.row];      
        return cell;   
    }
}   

确保将图像添加到新创建的 ImageView 中,不是 cell.imageView,因为后者是只读属性,位于左侧,我不确定你可以调整它的大小。

您还需要设置单元格的高度,因为它比标准高。这将在 tableView:heightForRowAtIndexPath: 委托(delegate)方法中。

关于iphone - UITableViewCell 带图片自定义尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7952334/

相关文章:

iphone - 如何让应用程序的设计兼容iPhone 4/iPhone 5/iPad?有捷径吗?

iphone - 在不重新加载整个 tableView 的情况下在 tableView 中实现 "More"按钮

iphone - 无法使用 iOS 4.3.5 设备在 Xcode 4.1 中构建源代码

ios - IGListKit - IGListSectionController 中的动态 .minimumLineSpacing

iphone - XCode iPhone OS 部署目标工具

ios - 使用导航栏和 UITextField 时出现问题

Xcode 7 扩展错误

ios - Kotlin Multiplatform Mobile 无法在 iOS 上运行 : Execution failed for task ':shared:compileKotlinIosX64'

ios - 每当我尝试将 NSUserDefualts 保存到 NSMutableArray 时,它就会崩溃

ios - 仅使用 SKStoreReviewController 或 iOS 中的替代品对应用进行评分