简体   繁体   English

自定义tableview单元格多行

[英]Custom tableview cell multiple rows

这是Samole的形象 This is my json data. 这是我的json数据。 Here it is Restaurant name coming one and line name coming 2 some times line name coming more then how to print the data in custom cell.please. 这里是餐厅名称一个,行名称两个,有时行名​​称更多,然后如何在自定义cell.please中打印数据。 help me 帮我

     "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)"
      },

Try like this: 尝试这样:

First get all values from your response in NSMutableArray what you want 首先从NSMutableArray的响应中获取所有值

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

After that in viewDidLoad add values to those mutable arrays which you got from your response 之后,在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);
    }

Now all you want is implement table view delegate methods like below: 现在,您所需要做的就是实现表视图委托方法,如下所示:

-(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];
}

Here I'm just populating restaurant names in tableView section header as NSString . 在这里,我只是在tableView节标题中将餐厅名称填充为NSString

If you want exactly like android what you shown above you have to implement below methods instead of -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section this method 如果您想完全像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;
}

Like the same way I'm just populating lineName in cell.textLabel 就像我在cell.textLabel中cell.textLabel

If you want to do more , just create custom tableViewCell and create layout how you want. 如果您想做更多事情,只需创建自定义tableViewCell并根据需要创建布局即可。

that's it. 而已。

Cheers. 干杯。

Use your restaurant array for section 将餐厅阵列用于部分

numberOfSectionsInTableView{}

and your "lines": [{}] array for 和您的"lines": [{}]数组

numberOfRowsInSection{}

You may get better idea from code mentioned below 您可能会从下面提到的代码中得到更好的主意

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

and for multiple rows considering you have restaurant as object: 对于多行,考虑您将餐厅作为对象:

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

First of all create an array of Restaurant type an fetch the data from json and add it into an array just like that 首先创建一个Restaurant类型的数组,然后从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)

}

now use this array in your tableview 现在在您的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
}

I hope this will make a lot of sense to you 我希望这对您有意义

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM