[英]Adding UIImageView to UITableViewCell?
我在将UIImageView添加到单个UITableView单元时遇到了麻烦。 在我的情节提要中,我有一个UITableViewController和四个原型动态单元格。 其中三个像正常的左细节单元一样好,并且工作正常。 但是其中之一我需要具有UIImageView。
因此,我给单元格添加了一个视图,给了单元格一个带有属性的自定义类,这样我就可以访问图像视图。
我有以下单元格:
标题单元格。
图像单元。
URL单元格。
笔记单元格。
表格视图的内容根据用户是否要添加URL,注释或图像而变化。 因此,用户总是会看到标题单元格与其他单元格之一。
这是我的代码,无论出于什么原因,我都无法使UIImageView显示在该图像单元上。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//0 = Image
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0)
{
if (indexPath.row == 0) CellIdentifier = @"titleCell";
if (indexPath.row == 1) CellIdentifier = @"imageCell";
if (indexPath.row == 2) CellIdentifier = @"notesCell";
}
//1 = URL
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1)
{
if (indexPath.row == 0) CellIdentifier = @"titleCell";
if (indexPath.row == 1) CellIdentifier = @"urlCell";
if (indexPath.row == 2) CellIdentifier = @"notesCell";
}
//2 = Notes
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2)
{
if (indexPath.row == 0) CellIdentifier = @"titleCell";
if (indexPath.row == 1) CellIdentifier = @"notesCell";
}
ScrapbookImageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ScrapbookImageDetailCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[cell setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:250.0f/255.0f alpha:1.0f]];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.text = [firstSection objectAtIndex:indexPath.row];
cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13.0f];
cell.detailTextLabel.numberOfLines = 0;
cell.accessoryView = nil;
switch (indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
cell.detailTextLabel.text = scrapbook.title;
}
break;
case 1:
{
//0 = Image
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0)
{
cell.detailTextLabel.text = nil;
cell.iv.image = [UIImage imageNamed:@"image.png"];
}
//1 = URL
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1)
{
cell.detailTextLabel.text = scrapbook.url;
}
//2 = Notes
else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2)
{
cell.detailTextLabel.text = scrapbook.notes;
}
}
break;
case 2:
{
cell.detailTextLabel.text = scrapbook.notes;
}
break;
default:
break;
}
break;
default:
break;
}
return cell;
}
为什么要更改标识符值? 您要做的就是创建自定义单元格,并在行高中检查是否是您想要的行,然后返回图像的高度,否则返回默认大小,在单元格中进行行方法,再次检查所需的行并添加图像您要添加其他,使imageview为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.