如何在ios视图中绑定table视图中的数组数据?
How to bind array data in table view in ios?
我有一个自定义 table 视图单元,在自定义 table 视图单元中有 4-5 张图像。我正在尝试在单元格中绑定数据,但图像在第一个单元格之后重复出现。我想要的只是将所有 url 的数组映射到我的 table 视图的所有单元格的 uiimageview,就像 google 图像的图像部分一样。
这是我想要实现的屏幕截图:
这是我目前得到的结果:
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
urlArray = [NSArray arrayWithObjects:
@"http://www.w3schools.com/images/w3schools_green.jpg",
@"https://farm1.staticflickr.com/2/1418878_1e92283336_m.jpg",
@"http://i.share.pho.to/68bd6b4c_o.jpeg",
@"http://i.share.pho.to/a9cf8966_o.jpeg",
@"http://i.share.pho.to/934e487c_o.jpeg",
@"http://i.share.pho.to/6343e165_o.jpeg",
@"http://i.share.pho.to/99a3ffd0_o.jpeg",
@"http://i.share.pho.to/9968ab24_o.jpeg",
@"http://i.share.pho.to/9968ab24_o.jpeg",
@"http://i.share.pho.to/a4cc0bac_o.jpeg",
nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FzTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSData * imageData1 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row]]];
NSData * imageData2 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row + 1]]];
NSData * imageData3 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+2]]];
NSData * imageData4 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row +3]]];
NSData * imageData5 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+4]]];
NSData * imageData6 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+5]]];
// Configure the cell...
cell.img1.image = [UIImage imageWithData: imageData1];
cell.img2.image = [UIImage imageWithData: imageData2];
cell.img3.image = [UIImage imageWithData: imageData3];
cell.img4.image = [UIImage imageWithData: imageData4];
cell.img5.image = [UIImage imageWithData: imageData5];
cell.img6.image = [UIImage imageWithData: imageData6];
return cell;
}
"cellForRowAtIndexPath" 配置单个单元格,并为列表中的每个单元格调用。因此,对于第一个单元格,它将使用数组索引
设置 6 个图像
[0,1,2,3,4,5]
第二次调用(对于第二个单元格)它将使用索引
[2,3,4,5,6]
要得到你所要求的,你必须使用:
int mappedIndex = indexPath.Row*6;
然后对于每个cellImage
cell.img1.image =mappedIndex;
cell.img1.image =mappedIndex+1;
cell.img1.image =mappedIndex+2;
等等
这样,单元格 1 的索引将是:
For cell 1: [0,1,2,3,4,5]
For cell 2: [6,7,8,9,10,11]
For cell 3: [12,13,14,15,16]
等
显然你需要一个更大的阵列才能工作。我认为你真的应该为这个想法使用 UICollectionView。 UICollectionView 处理 "Grid" 个单元格。然后它将为您提供有关何时按下每个单元格等的事件。
希望对您有所帮助。
如果你想通过使用 UICollectionView 来实现,你应该用集合视图替换你的 tableview 或者使用 UICollectionViewController。
它与 UITableview 非常相似,除了每个单元格都是 "grid" 单元格而不是行的一部分 - 它具有类似的委托回调来配置单元格,并获取每个部分的计数等。
我在这里解释的太多了,但是你可以按照这样的教程进行操作:http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
我相信该教程还解释了如何异步加载图像(因此下载图像的网络操作不会导致您的应用程序在下载过程中冻结)。
我有一个自定义 table 视图单元,在自定义 table 视图单元中有 4-5 张图像。我正在尝试在单元格中绑定数据,但图像在第一个单元格之后重复出现。我想要的只是将所有 url 的数组映射到我的 table 视图的所有单元格的 uiimageview,就像 google 图像的图像部分一样。
这是我想要实现的屏幕截图:
这是我目前得到的结果:
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
urlArray = [NSArray arrayWithObjects:
@"http://www.w3schools.com/images/w3schools_green.jpg",
@"https://farm1.staticflickr.com/2/1418878_1e92283336_m.jpg",
@"http://i.share.pho.to/68bd6b4c_o.jpeg",
@"http://i.share.pho.to/a9cf8966_o.jpeg",
@"http://i.share.pho.to/934e487c_o.jpeg",
@"http://i.share.pho.to/6343e165_o.jpeg",
@"http://i.share.pho.to/99a3ffd0_o.jpeg",
@"http://i.share.pho.to/9968ab24_o.jpeg",
@"http://i.share.pho.to/9968ab24_o.jpeg",
@"http://i.share.pho.to/a4cc0bac_o.jpeg",
nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FzTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSData * imageData1 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row]]];
NSData * imageData2 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row + 1]]];
NSData * imageData3 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+2]]];
NSData * imageData4 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row +3]]];
NSData * imageData5 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+4]]];
NSData * imageData6 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [urlArray objectAtIndex:indexPath.row+5]]];
// Configure the cell...
cell.img1.image = [UIImage imageWithData: imageData1];
cell.img2.image = [UIImage imageWithData: imageData2];
cell.img3.image = [UIImage imageWithData: imageData3];
cell.img4.image = [UIImage imageWithData: imageData4];
cell.img5.image = [UIImage imageWithData: imageData5];
cell.img6.image = [UIImage imageWithData: imageData6];
return cell;
}
"cellForRowAtIndexPath" 配置单个单元格,并为列表中的每个单元格调用。因此,对于第一个单元格,它将使用数组索引
设置 6 个图像[0,1,2,3,4,5]
第二次调用(对于第二个单元格)它将使用索引
[2,3,4,5,6]
要得到你所要求的,你必须使用:
int mappedIndex = indexPath.Row*6;
然后对于每个cellImage
cell.img1.image =mappedIndex; cell.img1.image =mappedIndex+1; cell.img1.image =mappedIndex+2;
等等
这样,单元格 1 的索引将是:
For cell 1: [0,1,2,3,4,5]
For cell 2: [6,7,8,9,10,11]
For cell 3: [12,13,14,15,16]
等 显然你需要一个更大的阵列才能工作。我认为你真的应该为这个想法使用 UICollectionView。 UICollectionView 处理 "Grid" 个单元格。然后它将为您提供有关何时按下每个单元格等的事件。
希望对您有所帮助。
如果你想通过使用 UICollectionView 来实现,你应该用集合视图替换你的 tableview 或者使用 UICollectionViewController。
它与 UITableview 非常相似,除了每个单元格都是 "grid" 单元格而不是行的一部分 - 它具有类似的委托回调来配置单元格,并获取每个部分的计数等。
我在这里解释的太多了,但是你可以按照这样的教程进行操作:http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
我相信该教程还解释了如何异步加载图像(因此下载图像的网络操作不会导致您的应用程序在下载过程中冻结)。