ios - 如何在 iOS 的 Tableview 的第一部分中设置数组数据第一个值,在 tableview 的第二部分中设置剩余的数组数据值?

标签 ios json uitableview

我是 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/

相关文章:

ios - NSAttributedString 行尾的 NSKernAttributeName 空格

php - 在 FullCalendar 4 中显示我的数据库中的事件

java - 放心: Size of json response

iphone - 设置自定义表格 View 单元格的背景图像

ios - 使用 Google place API 从 lat long 获取附近的地点

javascript - PushNotificationIOS.presentLocalNotification() 在 native react 中不起作用

javascript - 为什么 Google Closure Compiler 重命名对象的属性名称?

ios keyBoardShow 与 UITableView

iphone - 有没有模仿iPhone分组UITableView的CSS库?

ios - 我可以在 UIAlertView 的按钮中使用多行文本吗?