我是iOS开发的新手。我正在构建一个演示项目,其中有一个TableView和一个原型单元。我在该单元格中拍摄了四张图像视图。我有一组我有30张图片。所以,在我的代码中,我采用了一个数组来存储图像。现在我想以这样的方式填充我的表,在我的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
答案 0 :(得分:0)
尝试这个
-(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
}
}
}
答案 1 :(得分:0)
试试此代码,只需隐藏image3&amp;最后一行中的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;
}
答案 2 :(得分:0)
对于像我这样有要求的人来说,这就是解决方案..
#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