UITableView动态部分

时间:2013-09-12 13:11:11

标签: ios iphone ipad uitableview

我正在使用一个使用动态部分的tableview,我有一个sectionArray,它包含section title和另一个包含所有部分的整个数据的数组, 我无法弄清楚如何为每个部分写行的单元格,因为它是动态的

代码是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return [appDelegate.sectionArray count];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
  return [appDelegate.appNameArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  UILabel *AppNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(80, 0, 120, 30)];
  [AppNameLabel setBackgroundColor:[UIColor clearColor]];
  [AppNameLabel setText:[NSString stringWithFormat:@"%@",[appDelegate.appNameArray objectAtIndex:indexPath.row]]];
  [AppNameLabel setFont:[UIFont systemFontOfSize:14]];
  [cell.contentView addSubview:AppNameLabel];
  return cell;
}

在appname数组中我有包含所有部分的数据,我需要为特定部分隔离,如何在单元格中为行索引pls帮助。

提前致谢。

3 个答案:

答案 0 :(得分:2)

无需为sectionTitle和appName创建两个不同的array。使用NSArray个对象创建源NSDictionaryNSDictionary对象包含appName数组values,sectionTitle为key

sourceArray
(
 dictionaryObject1
 {
 key : sectionTitle,
 value : ( appName1,appName2,appName3)
 },
 dictionaryObject2
 {
 key : sectionTitle,
 value : ( appName1,appName2,appName3)
 },
.
.
.
)

答案 1 :(得分:0)

首先,您的numberOfRowsInSection方法应如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arrayForSection = appDelegate.sectionArray[section];
    return [arrayForSection count];
}

之后修改cellForRow方法:

NSArray *arrayForSection = appDelegate.sectionArray[indexPath.section]
[AppNameLabel setText:[NSString stringWithFormat:@"%@",[arrayForSection objectAtIndex:indexPath.row]]];

答案 2 :(得分:0)

您需要相应地更新数据源。只需通过枚举数组动态编辑数组。就像你添加了一个部分一样,你应该动态地在数组中添加部分细节。

NSArray *tempArray = [NSArray alloc] initWithArray:sectionArray];
for(int i = index; i<= (sectionArray.count-i-1); i++)
{
   if(i = index){
      [sectionArray replaceObjectAtIndex:i withObject:newSection]
   }
   else
   {
     sectionArray replaceObjectAtIndex:i withObject:[tempArray objectAtIndex: i-1];
   }
}