2016-02-16 3 views
1

Мне нужно изображение как в изображении в моем приложении. Я смущен, что использовать для этого, могу ли я использовать представление коллекции, или мне нужно создать свое представление с помощью view и imageViews и кнопки. Любая помощь или предложение будут оценены.Как создать несколько изображений?

+2

По моему предложению вы должны использовать UICollectionView с пользовательским Cell –

+0

для множественного выбора изображения вы можете использовать https://github.com/questbeat/QBImagePicker – kb920

+0

@ kb920 проверить изображение Мне нужно UIDesign в соответствии с изображением, а не с несколькими сторонними библиотеками. –

ответ

0

Используйте этот код.

ViewController.m

static CGFloat EdgeInsets; 
@property (strong, nonatomic) IBOutlet UICollectionView *collectionVeiw; 
@property (strong, nonatomic) NSMutableArray *arrImages; 

#pragma mark - View Life Cycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    EdgeInsets = 30; 
    _arrImages = [NSMutableArray alloc]init]; 
} 

#pragma mark - Collection View delegate methods 

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section{ 
    return _arrImages.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath]; 
    if (cell == nil) 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"ImageCell" owner:self options:nil] objectAtIndex:0]; 

    cell.imgView.image = [UIImage imageNamed:@"1.png"]; 
    [cell.btnDelete addTarget:self action:@selector(btnDeleteTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.btnDelete.tag = indexPath.row; 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 

} 

- (void)btnDeleteTapped:(UIButton*)sender{ 
    [_arrImages removeObjectAtIndex:sender.tag]; 
    [_collectionView reloadData]; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CGFloat width = [UIScreen mainScreen].bounds.size.width-(EdgeInsets); 
    return CGSizeMake(width/2, width/2); 
} 

Может быть, это полезно для вас.

+0

Дайте мне знать, если вам нужна помощь. –

1

Существует альтернативный способ сделать это с помощью пользовательской ячейки ...

1.You нужно взять UIImageView столько, сколько вы хотите .... (Здесь я взял 4 в ряд)

2.Выберите массив имен изображений. (которые могут быть получены, как мы получаем)

Теперь вы можете передать следующий UITableView DataSource код ....

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{   
    return arrImage.count %4 ? arrImage.count /4 +1 :arrImage.count /4; 
} 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *strCellIdentifier = @"Cell"; 
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier]; 
if (cell == nil) { 

    cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellIdentifier]; 
} 
NSUInteger imageIndex = (indexPath.row*4);//indexPath.row *(number of images on a row) 
//First image of the row 
    cell.image1.image = [UIImage imageNamed:[arrImage objectAtIndex:imageIndex +0]]; 
//Second image of the row 
if (arrImage.count > imageIndex+1){ 
    cell.image2.image = [UIImage imageNamed:[arrImage objectAtIndex:imageIndex +1]]; 
} 
//Third image of the row 
if (arrImage.count > (imageIndex+2)){ 
    cell.image3.image = [UIImage imageNamed:[arrImage objectAtIndex:imageIndex +2]]; 
} 
//Fourth image of the row 
if (arrImage.count > (imageIndex+3)){ 
    cell.image4.image = [UIImage imageNamed:[arrImage objectAtIndex:imageIndex +3]]; 
} 
cell.backgroundColor = [UIColor redColor]; 
return cell; 
} 
Смежные вопросы