我是iOS开发的新手。我的tableview中有一个自定义单元格。我有32个图像存储在名为Images的组中。现在我想以这样一种方式填充我的tableview,即我的单元格的一行包含4个图像。我只是有点困惑如何设计它。请帮帮我。
这是我的代码..
#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",@"32.jpg",@"31.jpg"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
_patternsString = [self.patternsArray objectAtIndex:indexPath.row];
UIImageView *imageView1 = (UIImageView*)[cell.contentView viewWithTag:1];
imageView1.image = [UIImage imageNamed:_patternsString];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.patternsArray count];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
This is how i have populated 2 images in one row you can follow these line to populate 4 image in one row , like take remainder of array count with 4 and if remainder is 0 then total number of rows will be array.count/4 .
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[Globals sharedInstance].couponsArray count]%2==0)
{
return [[Globals sharedInstance].couponsArray count]/2;
}
else
{
return [[Globals sharedInstance].couponsArray count]/2+1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell1";
// int currentRow = [indexPath row];
CustomCell1 *cell = (CustomCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell1" owner:self options:nil];
for (id object in nib) {
if([object isKindOfClass:[CustomCell1 class]])
cell = (CustomCell1*)object;
}
}
if (cell == nil)
{
cell = [[CustomCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(@"here is indexPath %d",indexPath.row);
int index= indexPath.row*1+indexPath.row;
int index1= index+1;
if ([Globals sharedInstance].couponsArray.count/2>=indexPath.row)
{
[cell.indicater1 stopAnimating];
[cell.indicater2 stopAnimating];
}
NSString* imageURLString = [[[Globals sharedInstance].couponsArray objectAtIndex:index] objectForKey:@"coupon_image"];
NSURLRequest* imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:60];
[cell.indicater1 setCenter:cell.carImage.center];
[cell.indicater1 startAnimating];
[cell.carImage setImageWithURLRequest:imageRequest
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[cell.indicater1 stopAnimating];
[cell.carImage setImage:image];
NSLog(@"Image must be Loaded here");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
[cell.indicater1 stopAnimating];
}];
[cell.btn1 setTag:index];
[cell.btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
if (index1>[Globals sharedInstance].couponsArray.count-1)
{
cell.carImage1.hidden=YES;
cell.lblModel_car1.hidden=YES;
cell.secondndImage.hidden=YES;
cell.indicater2.hidden=YES;
cell.indicater1.hidden=YES;
cell.triangleImage.hidden=YES;
}
else
{
NSString* imageURLString1 = [[[Globals sharedInstance].couponsArray objectAtIndex:index1] objectForKey:@"coupon_image"];
NSURLRequest* imageRequest1 = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString1]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:600];
[cell.indicater2 setCenter:cell.carImage1.center];
[cell.indicater2 startAnimating];
[cell.carImage1 setImageWithURLRequest:imageRequest1
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[cell.indicater2 stopAnimating];
[cell.carImage1 setImage:image];
NSLog(@"Image must be Loaded here");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
[cell.indicater2 stopAnimating];
}];
[cell.btn2 setTag:index1];
[cell.btn2 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
答案 1 :(得分:0)
为什么不设置uitableViewCell的图像视图? 试试这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
_patternsString = [self.patternsArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:_patternsString];
return cell;
}
希望它对你有所帮助。如果您正在设计自定义单元格(不使用tableViewCells的系统imageView),则可能需要创建自定义TableViewCell。
答案 2 :(得分:0)
you have to add 4 imageview into tableviewcell
为此你需要一个customtableviewcell 然后加 indexPath.row = 4 * indexPath.row; 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; imageView4.image = [UIImage imageNamed:[self.patternsArray objectAtIndex:indexPath.row + 3];