xcode UITableViewCell 带图片自定义大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7952334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableViewCell With image custom size
提问by TMB87
I'm loading an image in to an UITableView through a simple
我正在通过一个简单的方法将图像加载到 UITableView 中
cell.imageView.image = [UIImage imageNamed:@"prem.jpg"];
However, what I've learnt from my limited time with xcode things arent normally that easy! I want to have the image in there take up the fullsize of the image which is about 280 wide and 150 tall. From googling I might have to build a custom cell and put the image in there and then load that custom cell? I've not done anything like that before and it seems a bit annoying just to load one image. Is there an alternative?
然而,我从有限的时间里学到的东西通常没有那么容易!我想让那里的图像占据大约 280 宽和 150 高的图像的全尺寸。通过谷歌搜索,我可能必须构建一个自定义单元格并将图像放在那里,然后加载该自定义单元格?我以前没有做过这样的事情,只是加载一张图片似乎有点烦人。有替代方案吗?
To put in context, I have 3 sections in the tableview top section will be the image, and the other two are arrays loaded from XML files. If I do a custom cell I have to do a seperate xml call to get the image url before it goes through so I'd really like to avoid that if possible?
在上下文中,我在 tableview 顶部有 3 个部分将是图像,另外两个是从 XML 文件加载的数组。如果我做一个自定义单元格,我必须在它通过之前进行单独的 xml 调用来获取图像 url,所以如果可能的话,我真的想避免这种情况?
Really looking for any pointers you can offer. Thanks a lot
真的在寻找您可以提供的任何指示。非常感谢
Tom
汤姆
Edit: cellforrowindexpath as requsted:
编辑: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;
}
回答by jrturton
There is no need to create a custom cell subclass. I am assuming you have a single cell in section 0 and a variable number of cells in sections 1 and 2.
无需创建自定义单元子类。我假设您在第 0 节中有一个单元格,而在第 1 节和第 2 节中有可变数量的单元格。
You should be using a different cell reuse identifier for your cell in section 0 as it contains an image view and the others don't - this will show up in your later sections when the cells are recycled otherwise.
您应该在第 0 部分中为您的单元格使用不同的单元格重用标识符,因为它包含一个图像视图,而其他的不包含 - 当单元格被回收时,这将显示在您后面的部分中。
I'd try something like this:
我会尝试这样的事情:
-(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;
}
}
Make sure you are adding the image to the newly created image view and notcell.imageView as the latter is a read-only property which is over to the left side, I'm not sure you can resize it.
确保您将图像添加到新创建的图像视图而不是cell.imageView,因为后者是位于左侧的只读属性,我不确定您是否可以调整它的大小。
You will also need to set the height for your cell, as it is taller than the standard. This will be in the tableView:heightForRowAtIndexPath:
delegate method.
您还需要设置单元格的高度,因为它比标准高。这将在tableView:heightForRowAtIndexPath:
委托方法中。
回答by Tendulkar
you can use the customCells.Means take an image view (whatever size you want.).And add it to the cell in cellforIndexpath method.
您可以使用 customCells.Means 获取图像视图(无论您想要什么尺寸。)并将其添加到 cellforIndexpath 方法中的单元格中。