gpt4 book ai didi

ios - 如何使用多个项目填充 UITableViewCell?

转载 作者:行者123 更新时间:2023-11-29 02:14:31 33 4
gpt4 key购买 nike

我是 iOS 开发的新手。我的表格 View 中有一个自定义单元格。我有 32 张图片存储在一个名为 Images 的组中。现在我想以单元格的一行包含 4 个图像的方式填充我的表格 View 。我只是有点困惑如何设计它。请帮助我。

这是我的代码..

#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

最佳答案

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;


}

关于ios - 如何使用多个项目填充 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897728/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com