如何清除tableview(iPhone)中特定部分的(分隔符颜色)?

如何清除tableview(iPhone)中特定部分的(分隔符颜色)?,iphone,ios,uitableview,Iphone,Ios,Uitableview,目前,我正在使用iPhone应用程序,使用tableview开发表格及其样式,如分组,第2节的编号,然后第一节有分隔颜色,如浅灰色,第二节有分隔颜色,如clearColor。 但当我滚动表格视图时,有时第2节活动时,第1节也会清除一个分隔符颜色,如何解决这个问题?请任何人帮助我 提前谢谢 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

目前,我正在使用iPhone应用程序,使用tableview开发表格及其样式,如分组,第2节的编号,然后第一节有分隔颜色,如浅灰色,第二节有分隔颜色,如clearColor。 但当我滚动表格视图时,有时第2节活动时,第1节也会清除一个分隔符颜色,如何解决这个问题?请任何人帮助我

提前谢谢

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

       if (indexPath.section == 0 && indexPath.row == 0) 
        {
            studentUpdateTable.separatorColor = [UIColor lightGrayColor];
            cell.backgroundColor = [UIColor whiteColor];
        }
       else if(indexPath.section == 1 && indexPath.row == 0)
       {
            studentUpdateTable.separatorColor = [UIColor clearColor];
            cell.backgroundColor = [UIColor clearColor];
       }
}

您可以将其设置为“无”,而不是尝试将其设置为“clearColor”

这可以从界面生成器本身完成。。在表视图的属性检查器中,您可以选择分隔符样式。。。无需任何编码…

这确实有效

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

添加了自定义UIView(新分配的UIView)作为单元格的背景视图。

您的问题似乎是每次滚动时都在分配单元格,因为tableView重用单元格,所以无需每次分配。如果单元格为零,则只分配它

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) 
    {   
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier] autorelease];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

   if (indexPath.section == 0) 
   {
        studentUpdateTable.separatorColor = [UIColor lightGrayColor];
        cell.backgroundColor = [UIColor whiteColor];
   }
   else if(indexPath.section == 1)
   {
        studentUpdateTable.separatorColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
   }
}

但这也会得到相同的结果,只是一个注释:如果使用“dequeueReusableCellWithIdentifier”,则不必检查单元格是否为nil并手动初始化单元格。这是由系统自动完成的(这是单元出列的点)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) 
    {   
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier] autorelease];
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

   if (indexPath.section == 0) 
   {
        studentUpdateTable.separatorColor = [UIColor lightGrayColor];
        cell.backgroundColor = [UIColor whiteColor];
   }
   else if(indexPath.section == 1)
   {
        studentUpdateTable.separatorColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
   }
}