我是 iOS 的新手,我知道这个问题被问了很多次,但我没有得到解决方案。我用 tableview 创建了一个应用程序,这是我的 tableview 代码,包含两个部分,我想用 JSON 数据填充这个部分。我制作了两个 CustomCell。它显示良好,但我无法填写我的 JSON 解析数据。
我的代码是。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *CellIdentifierOne=@"CellOne";
CustumCell *cellOne =[tableView dequeueReusableCellWithIdentifier:CellIdentifierOne];
if (cellOne == nil)
{
NSArray *nibOne=[[NSBundle mainBundle]loadNibNamed:@"CustumCell" owner:self options:nil];
cellOne=[nibOne objectAtIndex:0];
cellOne.userInteractionEnabled=NO;
}
{
[cellOne.spinner startAnimating];
NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section];
NSString *img2=[dict valueForKey:@"front_image"];
[cellOne.storeImage sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Setting.png"] options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
{
[cellOne.spinner stopAnimating];
cellOne.spinner.hidesWhenStopped=YES;
}];
}
return cellOne;
}
else
{
static NSString *CellIdentifierTwo=@"CellTwo";
TableCell *cellTwo=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];
if (cellTwo == nil)
{
NSArray *nibTwo=[[NSBundle mainBundle]loadNibNamed:@"TableCell" owner:self options:nil];
cellTwo=[nibTwo objectAtIndex:0];
cellTwo.userInteractionEnabled=NO;
}
return cellTwo;
}
return nil;
}
这里的 Tableview 部分是两个,numberofRow 的代码也是
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
NSLog(@"Images %@",self.imageArray);
}
else
{
return self.imageArray.count - 1;
}
}
当没有任何数据时,它会按我想要的方式显示,但我无法填写我的数据请提供任何解决方案。
最佳答案
@AshishGabani
看看下面的代码,我对你的要求的理解
我取了一个数组,包含这样的值
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",nil];
下一个 TableView 方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) return 1;
return imageArray.count-1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"Simple";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.section==0) {
cell.textLabel.text = [imageArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [imageArray objectAtIndex:indexPath.row+1];
}
return cell;
}
只需评论您现有的代码并复制此代码并粘贴您的 Xcode ViewController 并运行模拟器,我想我达到了您的要求。
关于ios - 如何在 iOS 的 Tableview 的第一部分中设置数组数据第一个值,在 tableview 的第二部分中设置剩余的数组数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26728765/