0

我正在为我的 UITableview 使用一个自定义单元格,其中我有几个 UILabel。在一个标签中,我将它的颜色设置为红色。这对我来说效果很好。但是,一旦我通过 Egorefresh 表委托方法重新加载 TableView,其他单元格的标签颜色也开始变为红色。我不知道为什么会出现这个问题,这让我很反感。

这是 TableView 数据源方法 cellForRowAtIndexPath 的代码:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
  forRowAtIndexPath:(NSIndexPath *)indexPath{

    // Set the Frame for BackgroundView of Cell.
    NSDictionary * insuranceDic = [arr_insurance objectAtIndex:indexPath.row];
    insuranceDic = [[AppDelegate sharedInstance] removeNullsFromDictonary:insuranceDic];
    InsuranceCell * mycell = (InsuranceCell *)cell;
    if([[insuranceDic valueForKey:@"active"] isEqualToString:@"N"]){
        //TTTRegexAttributedLabel * label = [[TTTRegexAttributedLabel alloc] init];
        mycell.lbl_insuranceName.text =  [NSString stringWithFormat:@"%@ (%@)",
       [insuranceDic valueForKey:@"insurance_name"],@"Inactive"];
        mycell.lbl_insuranceName.textColor = [UIColor redColor];
        //[label setText:cell.lbl_insuranceName.text withRegex:@"(Inactive)" withFont: 
    [UIFont boldSystemFontOfSize:12] withColor:[UIColor redColor]];
    }
    else{
        mycell.lbl_insuranceName.text = [insuranceDic valueForKey:@"insurance_name"];
    }
}

从这些方法重新加载表视图时,问题正在发生:

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// For Top Pull to refresh.
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
   //For Bottom Pull to refresh.
    [pullToBottomRefreshManager_ tableViewScrolled];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate: 
  (BOOL)decelerate{
    // For Top Pull to refresh.
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    //For Bottom Pull to refresh.
    [pullToBottomRefreshManager_ tableViewReleased];
}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

// Belowline is for stop refresh indicator
//[_refreshHeaderView 
  egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    // When we going to refresh the data we need to reset the start varible to 1
    // and also removeallthe previous objects from the array.
    str_start = @"1";
    [self getInsuranceList];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
      return isReloading; // should return if data source model is reloading
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated: 
  (EGORefreshTableHeaderView*)view{

      return [NSDate date]; // should return date data source was last changed

}

#pragma mark -
#pragma mark MNMBottomPullToRefreshManagerClient Methods
- (void)bottomPullToRefreshTriggered:(MNMBottomPullToRefreshManager *)manager {

   // Here we are incrementing start with records per page means start+=recordsperpare 
   or start+=20.
   str_start = [NSString stringWithFormat:@"%d",[str_start intValue]+
   [str_recordsPerPage intValue]];
   [self getInsuranceList];
}

这是 cellForRowAtIndexPath 方法的代码:

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

    }
    NSDictionary * insuranceDic = [arr_insurance objectAtIndex:indexPath.row];
    insuranceDic = [[AppDelegate sharedInstance] removeNullsFromDictonary:insuranceDic];
    cell.lbl_insuranceNo.text = [insuranceDic valueForKey:@"insurance_no"];
    cell.lbl_groupNo.text = [insuranceDic valueForKey:@"group_no"];
    cell.lbl_priority.text = [self getPriorityDescFromCode:[insuranceDic    
    valueForKey:@"priority"]];
    cell.lbl_startDate.text = [insuranceDic valueForKey:@"start_date"];
    cell.lbl_endDate.text = [insuranceDic valueForKey:@"end_date"];
    cell.lbl_copay.text = [insuranceDic valueForKey:@"copay"];

   // This Condition is For setting alternate (White/SkyBlue) Background color for cell.
   //    if (fmod(indexPath.row, 2)==0) {
   //        cell.backgroundView.backgroundColor = [UIColor colorFromRGBIntegers:237       green:243 blue:249 alpha:1];
   //        
   //    }
   //    else{
   //        cell.backgroundView.backgroundColor = [UIColor colorFromRGBIntegers:250   green:250 blue:250 alpha:1];
   //    }

   cell.selectionStyle = UITableViewCellEditingStyleNone;
   return cell;
 }
4

1 回答 1

0

如果您可以添加您的cellForRowAtIndexPath:实现,这将有所帮助。

鉴于您提供的信息,我想您正在重用单元格而不是之前重置颜色。- (void)prepareForReuse您需要覆盖InsuranceCell 并重置任何单元格特定属性。

示例实现:

- (void)prepareForReuse {
    [super prepareForReuse];
    // reset the label text color to your default color (e.g. black)
    self.lbl_insuranceName.textColor = [UIColor blackColor];
}

您需要在InsuranceCell中实现此方法。

您需要了解,cellForRowAtIndexPath:您的 TableViewCells 的默认实现将被重用。这意味着您需要在重用单元之前显式重置每个属性。最方便的方法是覆盖该prepareForReuse方法,如上所示。

于 2013-04-05T12:51:05.817 回答