1

I'm using a UITableView to show sth,and the information in section 1,section 2 will not change,but section 3 is dynamic.

Is there any way to show section 1&2 using static cell,but I can write the datasource of section3?

Rick
  • 235
  • 4
  • 13
  • Yes. This is answered elsewhere on Stack Overflow. – Aaron Brager Apr 14 '15 at 04:11
  • 1
    May following links will help you. http://stackoverflow.com/questions/9322885/combine-static-and-prototype-content-in-a-table-view – Jatin Patel - JP Apr 14 '15 at 05:32
  • Thanks all and I've found the answer at http://stackoverflow.com/questions/12858363/uitableview-handle-cell-selection-in-a-mixed-cell-table-view-static-and-dynamic/12859177#12859177 The method [super tableView...] is worked for me. – Rick Apr 14 '15 at 07:44

2 Answers2

2

Here you will get as per your requirement.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0 || section == 1) {
    return 1; //However many static cells you want
} else {
    return [_yourArray count];
}
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0 || indexPath.section == 1 ) {
    NSString *cellIdentifier = @"staticCellType";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = @"some static content";        
    return cell;

} else if (indexPath.section == 1){

    NSString *cellIdentifier = @"dynamicCellType";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row];
    return cell;

} 
return nil;

}
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
0

You can easily do like this....

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     return 3; //number of section
 }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
     switch(section){
     case 0:
        return 1; //static value as per your requirement
      break;
     case 1:
       return 1; //static value as per your requirement
     break;
     case 2:
       return [yourArray count]; //static value as per your  requirement
     break;
    default
      return 1;

  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"CELL";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   if (indexPath.section == 2) {
      NSDictionary *dict = [yourArray objectAtIndex:indexPath.row];
   }

 //If you perform any task for other section then you can write in switch case

 return cell;

 }
Bhoomi Jagani
  • 2,413
  • 18
  • 24