2013-04-05 35 views
0

我正在使用一個自定義單元格用於我的UITableview,其中有幾個UILabels。在一個標籤中,我將它的顏色設置爲紅色,這對我來說工作得很好。但只要我通過Egorefresh Table委託方法重載TableView方法,其他單元格的標籤顏色也開始變爲紅色。我不知道爲什麼會出現這個問題,它給我造成了很多的厭惡。自定義單元格在同一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; 
} 
+3

其中是你的cellForRowAtIndexPath方法? – iPatel 2013-04-05 12:48:24

+0

代碼首先在cellForRowAtIndexPath方法中完成,但同樣的錯誤出現在那裏。所以我在willDisplayCell中編碼,並錯誤地將它命名爲cellForRowAtIndexPath。所以請爲上面的代碼建議一些東西? – Shikhar 2013-04-05 12:53:31

回答

0

這會幫助你,如果你coul d添加您的cellForRowAtIndexPath:實施。

鑑於你提供的信息,我想你是重複使用單元格而不是重新設置顏色。 您需要在InsuranceCell 中覆蓋- (void)prepareForReuse,並重置所有特定於單元格的屬性。

示例實現:

- (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方法。

+0

這是我的cellForRowAtIndexPath方法代碼:你可以看看它,我明白了關於重用單元的想法,但不瞭解如何去除這個異常。 – Shikhar 2013-04-05 12:56:12

+0

重寫您的InsuranceCell中的'pepareForReuse'方法。 – 2013-04-05 12:57:36

+0

G.你能解釋一下在該方法中寫什麼以及在哪裏實現該方法? – Shikhar 2013-04-05 13:04:12

相關問題