根据我的要求填充 table

Populating the table as per my requirement

我是 iOS 开发新手。我正在构建一个演示项目,其中有一个 TableView 和一个原型单元格。我在那个单元格中拍摄了四个图像视图。我有一个小组,里面有 30 张图片。因此,在我的代码中,我采用了一个数组来存储图像。现在我想以这样的方式填充我的 table,在我的 TableView 中,将有 8 行。每行将包含 4 张图像,但最后一行,即第 8 行将包含 2 张图像。

但是当我 运行 我的代码最后一行也显示 4 张图片而不是 2 张。谁能告诉我我哪里错了?提前致谢。

这是我的代码...

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

 self.patternsArray = @[@"1.jpg",@"3.jpg",@"2.jpg",@"4.jpg",@"6.jpg",@"5.jpg",@"7.jpg",@"9.jpg",@"8.jpg",@"10.jpg",@"12.jpg",@"11.jpg",@"13.jpg",@"15.jpg",@"14.jpg",@"16.jpg", @"18.jpg",@"17.jpg",@"19.jpg",@"21.jpg",@"20.jpg",@"22.jpg",@"24.jpg",@"23.jpg", @"25.jpg",@"27.jpg",@"26.jpg",@"28.jpg",@"30.jpg",@"29.jpg"];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];

UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];

UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];

for (NSInteger i=0; i<=[self.patternsArray count]; i++) {
    UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
    imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];

    UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
    imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3]];
}

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{



return [self.patternsArray count]/3.75;

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

试试这个

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

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

UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];

UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
if(indexPath.row != 7){

    UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
    imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
    imageView3.hidden=NO;

    UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
    imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3]];
    imageView4.hidden=NO;

}
else
{
    UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
    imageView3.hidden=YES;

    UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
    imageView4.hidden=YES;
}
return cell;
}

自定义图片

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

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

    if(indexPath.row !=7){
        UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
        imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
        imageView1.hidden =NO;

        UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
        imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
        imageView2.hidden =NO;

        UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
        imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
        imageView3.hidden=NO;

        UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
        imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3]];
        imageView4.hidden=NO;

    }
    else{
        if(patternsArray%4 == 1){
            UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
            imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
            imageView1.hidden=NO;

            UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
            imageView2.hidden=YES;


            UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
            imageView3.hidden=YES;

            UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
            imageView4.hidden=YES;
        }
        else if (patternsArray%4 == 2){
            UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
            imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
            imageView1.hidden=NO;

            UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
            imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
            imageView1.hidden=NO;

            UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
            imageView3.hidden=YES;

            UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
            imageView4.hidden=YES;
        }
        else if (patternsArray%4 == 3){
            UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
            imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
            imageView1.hidden=NO;

            UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
            imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
            imageView2.hidden=NO;

            UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
            imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
            imageView3.hidden=YES;

            UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
            imageView4.hidden=YES;
        }
        else{
            //you requirement
        }
    }
}

试试这个代码,只需在最后一行隐藏 image3 和 image4 视图

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];

    UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
    imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:(indexPath.section * 4)+indexPath.row]];

    UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
    imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:(indexPath.section * 4)+indexPath.row]];

    if(indexPath.row != 7){ {
        UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
        imageView3.image = [self.patternsArray objectAtIndex:(indexPath.section * 4)+indexPath.row];

        UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
        imageView4.image = [self.patternsArray objectAtIndex:(indexPath.section * 4)+indexPath.row];
        imageView3.hidden=NO;
        imageView4.hidden=NO;
    }
        else {
            UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
            UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];
            imageView3.hidden=YES;
            imageView4.hidden=YES;
        }

    return cell;
}

对于那些和我一样有需求的人,这就是解决方案..

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

 self.patternsArray = @[@"1.jpg",@"3.jpg",@"2.jpg",@"4.jpg",@"6.jpg",@"5.jpg",@"7.jpg",@"9.jpg",@"8.jpg",@"10.jpg",@"12.jpg",@"11.jpg",@"13.jpg",@"15.jpg",@"14.jpg",@"16.jpg", @"18.jpg",@"17.jpg",@"19.jpg",@"21.jpg",@"20.jpg",@"22.jpg",@"24.jpg",@"23.jpg", @"25.jpg",@"27.jpg",@"26.jpg",@"28.jpg",@"30.jpg",@"29.jpg", @"31.jpg",@"32.jpg"];
}

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


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

UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
UIImageView *imageView2 = (UIImageView*)[cell.contentView viewWithTag:2];
UIImageView *imageView3 = (UIImageView*)[cell.contentView viewWithTag:3];
UIImageView *imageView4 = (UIImageView*)[cell.contentView viewWithTag:4];

if ([self.patternsArray count]%4 == 0) {
    imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
    imageView1.hidden=NO;

    imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
    imageView2.hidden=NO;


    imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
    imageView3.hidden=NO;

    imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3]];
    imageView4.hidden=NO;
}

else if([self.patternsArray count]%4 != 0 && indexPath.row != [self.patternsArray count]/4){
    imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
    imageView1.hidden=NO;

    imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
    imageView2.hidden=NO;


    imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
    imageView3.hidden=NO;

    imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+3]];
    imageView4.hidden=NO;


}
else
{
    if ([self.patternsArray count]%4 == 1) {
        imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
        imageView1.hidden=NO;
        imageView2.hidden=YES;
        imageView3.hidden=YES;
        imageView4.hidden=YES;
    }
    if ([self.patternsArray count]%4 == 2) {
        imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
        imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
        imageView1.hidden=NO;
        imageView2.hidden=NO;
        imageView3.hidden=YES;
        imageView4.hidden=YES;
    }
    if ([self.patternsArray count]%4 == 3) {
        imageView1.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row]];
        imageView2.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+1]];
        imageView3.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row+2]];
        imageView1.hidden=NO;
        imageView2.hidden=NO;
        imageView3.hidden=NO;
        imageView4.hidden=YES;
    }
}

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (_patternsArray.count % 4==0) {
    return _patternsArray.count/4;
}
return [self.patternsArray count]/4 + 1 ;

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end