我在将 UIImageView 添加到单个 UITableView 单元格时遇到了真正的麻烦。在我的故事板中,我有一个 UITableViewController 和四个原型动态单元格。其中三个与正常的左侧细节单元格一样好,并且工作正常。但是其中一个我需要有一个 UIImageView 。
所以我在单元格中添加了所说的视图,给单元格一个带有属性的自定义类,这样我就可以访问图像视图。
我有以下单元格:
标题单元格。
图像单元。
网址单元格。
笔记单元格。
表格视图的内容会根据用户是否要添加 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;
}