开发者

How to create a section table view which depends on the table view data in iPhone?

开发者 https://www.devze.com 2023-02-07 06:37 出处:网络
I want to create a sectioned table view. Using XML parsing, i got the datas and displayed in the table view. Now i want to check, whether the data is empty, then it shouldn\'t create the table view se

I want to create a sectioned table view. Using XML parsing, i got the datas and displayed in the table view. Now i want to check, whether the data is empty, then it shouldn't create the table view section. so i want to check it before create the table view. Which means, if the data is not empty then should create a section, otherwise can't create a section. SO the table view section only depends on the data(Empty or Not).

Here my sample code,

 person = [[Person alloc] initWithPersonName:DictionaryValue]; // Passing the values via Dictionary.
 [self.navigationController pushViewController:person animated:YES];

In Person Class:

-(Person *) initWithPersonName:(NSMutableDictionary *) personDict  
{
  self.tableView.delegate = self;

      [[NSBundle mainBundle] loadNibNamed:@"Person" owner:self options:nil];

       NSString *firstName = [personDict valueForKey:@"firstName"];

       NSString *lastName = [personDict valueForKey:@"lastName"];

       NSString *cityName = [personDict valueForKey:@"city"];

       NSString *stateName = [personDict valueForKey:@"state"];

       return self;
 }

I want to create the two sections,

                  section - 1 -- > [firstName, LastName];
                  section - 2 -- > [city, state];

How can i put those section values into the arrays. Is possible to create a single array to use both sections or Need to create a separate array for store the s开发者_如何学Ceparate sections.

Suppose i get the empty value for any data, so it shouldn't create a table view section. What are the possibilities to achieve this? Please guide me.

Please give me any sample code or sample link.

Thanks.


You need to implement -numberOfSectionsInTableView:, -tableView:titleForHeaderInSection:, and -tableView:numberOfRowsInSection: in your UITableViewController subclass (create one if you don't already have one). The names are pretty self-explanatory. So you could do something like:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 0) return @"Title of section 1";
    if (section == 1) return @"Title of section 2";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // return the number of rows you've got based on the section index
}

That, I believe, will create the section headers regardless of whether you've got rows in them or not, but nothing will show beneath it. So it will be clear that a section is empty if it has no data. Make sense?

Cody

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号