有条件地将图像添加到UITableViewCell

时间:2014-08-06 00:32:26

标签: ios objective-c arrays uitableview

我正在尝试有条件地将图片添加到我的UITableViewCell。用户将添加照片,然后将照片添加到我的photoArray数组中。我遇到的问题是图像不断添加。我不想在我的阵列中添加空白图像。我会附上一些代码。

这是我的(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法:

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
self.photoImage = image;
[self.photoArray addObject:self.photoImage];
[self.tableView reloadData];
[picker dismissViewControllerAnimated:YES completion:nil];

继承我的(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];

XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Poiret One" size:18];
cell.textLabel.text = toDoItem.itemName;

NSDictionary *dict = [self.tableArray objectAtIndex:indexPath.row];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.text = [dict objectForKey:@"Time"];

if (indexPath.row<[self.photoArray count]) {
    UIImage *photo = [self.photoArray objectAtIndex:indexPath.row];
    cell.imageView.image = photo;
}

if (toDoItem.completed) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;

1 个答案:

答案 0 :(得分:0)

很难从您的描述中说出来,但据我从您的源代码中可以理解 - 您有一系列ToDo项目。不知何故,你可以将图片分配给一些待办事项,并且你想要显示它。

您可能应该做的是将此图片(UIImage)作为模型的一部分。添加到您的XYZToDoItem

@property (strong, nonatomic) UIImage *photo;

然后,当UIImagePickerController返回一张图片时 - 将其分配给相应的XYZToDoItem

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
self.photoImage = image; // Not sure why do you need that....
XYZToDoItem *currentItem = self.toDoItems[indexPath.row];
currentItem.photo = image;
[self.tableView reloadData];
[picker dismissViewControllerAnimated:YES completion:nil];

但是您需要在此方法中选择行indexPath。我不确定你如何展示你的UIImagePickerController