自定义单元格正在复制同一Tableview中其他单元格的属性

时间:2013-04-05 12:45:00

标签: ios objective-c uitableview

我正在为我的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;
 }

1 个答案:

答案 0 :(得分: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方法,如上所示。