ios - 自定义表格 View 单元格多行

标签 ios objective-c uitableview ipad

This is samole image 这是我的 json 数据。这里是餐厅名称为 1,线路名称为 2,有时线路名称更多,然后如何在自定义单元格中打印数据。请。帮帮我

     "currency": "$",
    "state": "sale",
    "total": 243.1,
    "name": "a1238",
    "restaurant_name": "\"Food Court\" Biergarten",
    "date": "2016-10-16 07:52:07",
    "table_no": null,
    "so_id": 238,
    "lines": [
      {
        "line_status": "pending",
        "line_id": 2536,
        "line_price": 1,
        "line_qty": 1,
        "line_name": "Käse"
      },
      {
        "line_status": "pending",
        "line_id": 2579,
        "line_price": 7.8,
        "line_qty": 2,
        "line_name": "3 Musketiere (3x verschiedene Hefe 0,3l)"
      },

最佳答案

像这样尝试:

首先从你在 NSMutableArray 中的响应中获取你想要的所有值

@interface ViewController ()
{
    NSMutableArray *restaurentsNamesArray;
    NSMutableArray *linesArray;
    NSMutableArray *linesCountArray;
}

之后在 viewDidLoad 中将值添加到您从响应中获得的那些可变数组

    - (void)viewDidLoad {
        [super viewDidLoad];

        restaurentsNamesArray = [[NSMutableArray alloc]init];

        linesArray = [[NSMutableArray alloc]init];

        linesCountArray = [[NSMutableArray alloc]init];
        NSError *error;

  // Do the stuff for get response from url here.
  // And give that request below.

        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
                NSDictionary *main = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                NSDictionary *result = [main valueForKey:@"result"];
                NSArray *saleorders = [result valueForKey:@"saleorders"];
                for (NSDictionary *dict in saleorders){
                    [restaurentsNamesArray addObject:[dict valueForKey:@"restaurant_name"]];
                    [linesArray addObject:[dict valueForKey:@"lines"]];
                }

        NSLog(@"%@",restaurentsNamesArray);
        NSLog(@"%@",linesArray);
    }

现在您所需要的就是像下面这样实现 TableView 委托(delegate)方法:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [restaurentsNamesArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array;
    if (linesArray.count > section){
        array = [linesArray objectAtIndex:section];
    }
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];

    NSArray *array;
    array = [linesArray objectAtIndex:indexPath.section];
    NSDictionary *dict = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict valueForKey:@"line_name"];
    cell.detailTextLabel.text = [dict valueForKey:@"line_id"];
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [restaurentsNamesArray objectAtIndex:section];
}

在这里,我只是将餐厅名称填充到 tableView 部分标题中作为 NSString

如果你想要像上面显示的 android 一样,你必须实现下面的方法而不是 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 这个方法

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];
    [headerView setBackgroundColor:[UIColor darkGrayColor]];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake((headerView.frame.size.width/2)-47,-32,300,94)];
    tempLabel.backgroundColor=[UIColor clearColor];
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor whiteColor];
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
    tempLabel.font = [UIFont boldSystemFontOfSize:17.0];
    tempLabel.text= [restaurentsNamesArray objectAtIndex:section];

    [headerView addSubview:tempLabel];

    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 35;
}

就像我只是在 cell.textLabel 中填充 lineName 一样

如果您想做更多,只需创建自定义 tableViewCell 并根据需要创建布局。

就是这样。

干杯。

关于ios - 自定义表格 View 单元格多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40127080/

相关文章:

ios - 切换 NSPersistentStore 文件时的 Core Data 延迟

ios - 是否可以从我的应用程序启动 Apple Pay NFC?

ios - 在 C++ 中将一个指针(是函数参数)复制到另一个指针(全局变量)

iphone - UITableViewCell 的 viewDidAppear

ios - 具有多个部分的 UITableView 中的原生广告

android - 使用 MediaCodec 编码时,三分之二的屏幕为绿色

ios - 当应用程序后台运行然后恢复时,带有 HTML5 数据库的 UIWebView 不会加载

ios - 损坏的应用程序,UIViewcontroller EXC_BAD_ACCESS 在 vi​​ewWillAppear 之后

objective-c - 如何从 NSMutableAttributedString 获取 NSParagraphStyle 属性名称?

iphone - 滚动uitableview时图像恢复为原始图像