gpt4 book ai didi

iphone - UITableViewCell 带图片自定义尺寸

转载 作者:行者123 更新时间:2023-11-28 18:14:17 25 4
gpt4 key购买 nike

我正在通过简单的方式将图像加载到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com