2014-02-04 4 views
-1

У меня есть NSMutableArray *_items. с 3 свойствами. ярлык (* текст) имеют бирку 1000. image 1001.Как показать UIImage в UITableView в IOS

@property (nonatomic, copy) NSString *text; 
@property (nonatomic, assign) BOOL checked; 
@property (nonatomic, copy) UIImage *image; 

проблема в том, что я не могу отобразить изображение.

_items = [[NSMutableArray alloc] initWithCapacity:20]; 
ChecklistItem *item; 
item = [[ChecklistItem alloc] init]; 
item.text = @"Walk the dog"; 
item.checked = NO; 
item.image = [UIImage imageNamed:@"img1.png"]; 

остальной код

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]; 
    ChecklistItem *item = _items[indexPath.row]; 
    UILabel *label = (UILabel *)[cell viewWithTag:1000]; 
    UIImage *image = (UIImage *)[cell viewWithTag:1001]; 
    image.image = item.image; 
    label.text = item.text; 
    [self configureCheckmarkForCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

ответ

1

Вы должны использовать UIImageView для отображения изображений, не UIImage:

@property (nonatomic, strong) UIImageView *imageView; 

использовать также атрибут strong свойства для этих свойств, а не copy.

2

В вашей cellForRowAtIndexPath: реализация вы используете UIImage, как если бы это было представление, а это не так. Вероятно, вы использовали UIImageView.

2

Вам нужно сделать пользовательский класс клеток и подключить свойства там , а затем в cellForRowAtIndexPath вы можете установить эти свойства

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomCell *cell = (AddListTableViewCell *)[tableView 
                  dequeueReusableCellWithIdentifier:@"CustomCell"]; 
if(cell==nil) 
{ 
    cell = (CustomCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"]; 
} 

cell.myText.text = @"String1" 
cell.myUmage.image = [UIImage imageNamed:@"one.png"]; 



return cell; 

} 
0

Почему вы пытаетесь сохранить UIImage?. Просто измените это свойство на NSString и сохраните имя изображения.

@property (nonatomic, copy) NSString *imageName; 

Затем в методе делегата вы можете создать UIImage с этим именем изображения и setImage в UIImageView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"ChecklistItem"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     // Configure the cell... 
     ChecklistItem *item = _items[indexPath.row]; 
     UILabel *label = (UILabel *)[cell viewWithTag:1000]; 
     UIImageView *imageView = (UIImageView *)[cell viewWithTag:1001]; 
     imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",item.imageName]]; 
     label.text = item.text; 
     return cell; 
    } 
0

Добавьте эту строку:

cell.imageView.image = [UIImage imageNamed:@"Your Image Here"]; 
Смежные вопросы