我可以在static-cell-UITableView中动态添加单元格吗?

时间:2015-04-14 04:09:04

标签: ios swift

我正在使用UITableView来显示某些内容,第1部分第2节中的信息不会改变,但第3部分是动态的。

有没有办法用静态单元格显示第1节和第2节,但是我可以写第3节的数据源吗?

2 个答案:

答案 0 :(得分:2)

您可以根据自己的要求获得。

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

}

答案 1 :(得分:0)

你可以轻松地做到这一点......

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

 }