【问题标题】:Custom tableview cell multiple rows自定义表格视图单元格多行
【发布时间】:2017-02-28 20:08:36
【问题描述】:

这是我的 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)"
      },

【问题讨论】:

  • 你能解释一下吗?这个描述真的很难帮你。
  • 这个问题是滚动后出现的吗?
  • 这个截图来自?我可以看到双网
  • 不,我的问题是如何打印与附件图像相同的数据。
  • 我猜这是后端的问题。不是从前端。请检查您来自后端的完整回复。

标签: ios objective-c uitableview ipad


【解决方案1】:

试试这样:

首先从您在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);
    }

现在你想要的只是实现如下表视图委托方法:

-(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 并根据需要创建布局。

就是这样。

干杯。

【讨论】:

    【解决方案2】:

    首先创建一个餐厅类型的数组,然后从 json 中获取数据并将其添加到一个数组中

    var RestaurantArray = [Restaurant]()
    
    for index  in 0..<JSON["data"].count
    {
    let address = String(JSON["data"][index]["Address"])
    
                let CityId = JSON["data"][index]["CityId"].int
    
                let Description = String(JSON["data"][index]["Description"])
    
                let Name = String(JSON["data"][index]["Name"])
    //create an object of Restaurant type and map the data into object and then add it into array that we have created .
    
    var RestaurantModel:Restaurant?
    RestaurantModel.address = address
    RestaurantModel.cityID =  cityId
    RestaurantModel.Description = Description
    
    RestaurantArray.append(RestaurantModel)
    
    }
    

    现在在你的 tableview 中使用这个数组

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return RestaurantArray.count
    }
    

    我希望这对你很有意义

    干杯

    【讨论】:

    • 我正在使用目标 c。
    • 尝试将其转换为目标 c 。它是一个简单的循环和数组声明
    【解决方案3】:

    使用您的餐厅数组作为部分

    numberOfSectionsInTableView{}
    

    还有你的"lines": [{}]数组

    numberOfRowsInSection{}
    

    你可能会从下面提到的代码中得到更好的主意

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    // Return the number restaurant count
    return [restaurantArray count];
    }
    

    考虑到您将餐厅作为对象,对于多行:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    // Return the number of lines count.
    return [objRestaurant.Lines count];
    }
    

    【讨论】:

    • 如果他从 json 获得多个餐厅?
    • 这不是他想要的吗?如果 json 中有 2 家餐厅,每个休息区各有 2-2 行。然后他期望在 tableview 中有 4 行。不是@Siva Sankar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多