我将渐变应用于具有动态高度的单元格。但对于某些单元格,渐变未正确应用。这是我的代码
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// If data source is not empty, then return one more cell space.
// (for displaying the "Loading More..." text)
if (section == SELLER_FEEDBACK_SECTION){
if ([self.sellerFeedbacksArray count] == 0)
return 0;
else
DLog(@" self.sellerFeedbacksArray count %d",[self.sellerFeedbacksArray count]);
return ([self.sellerFeedbacksArray count]);
}
else if (section == BUYER_FEEDBACK_SECTION){
if ([self.buyerFeedbacksArray count] == 0)
return 0;
else
DLog(@" self.buyerFeedbacksArray count %d",[self.buyerFeedbacksArray count]);
return ([self.buyerFeedbacksArray count]);
}
return 0;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *feedbackTextLabel = (UILabel*)[cell viewWithTag:FEEDBACK_TEXT_TAG];
[feedbackTextLabel setBackgroundColor:[UIColor clearColor]];
//Add Gradient to cell background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[RGBA(212.0, 208.0, 197.0, 1.0)CGColor],(id)[RGBA(166.0, 160.0, 142.0, 1.0)CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel* feedbackTextLabel;
UILabel* feedbackByUserLabel;
UIView* feedbackRatingsView;
UILabel* feedbackCountNoLabel;
float height;
static NSString* CellIdentifier = @"cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
RSTapRateView *tapRateView = nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor yellowColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
cell.selectionStyle= UITableViewCellSelectionStyleNone;
feedbackCountNoLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 21.0)];
feedbackCountNoLabel.backgroundColor = [UIColor clearColor];
feedbackCountNoLabel.font = [UIFont fontWithName:SEGOEUI size:14.0];
feedbackCountNoLabel.tag = FEEDBACK_COUNT_TAG;
[cell.contentView addSubview:feedbackCountNoLabel];
feedbackTextLabel = [[UILabel alloc]initWithFrame:CGRectZero];
feedbackTextLabel.font = [UIFont fontWithName:SEGOEUI size:14.0] ;
feedbackTextLabel.tag = FEEDBACK_TEXT_TAG;
[cell.contentView addSubview:feedbackTextLabel];
feedbackTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
feedbackTextLabel.numberOfLines = 0;
feedbackTextLabel.baselineAdjustment = UIBaselineAdjustmentNone;
feedbackByUserLabel = [[UILabel alloc]init];
feedbackByUserLabel.backgroundColor = [UIColor clearColor];
feedbackByUserLabel.font = [UIFont fontWithName:SEGOEUI size:14.0];
feedbackByUserLabel.tag = FEEDBACK_BY_USER_TAG;
feedbackByUserLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:feedbackByUserLabel];
feedbackRatingsView = [[UIView alloc]init];
feedbackRatingsView.backgroundColor = [UIColor clearColor];
feedbackRatingsView.tag = FEEDBACK_RATINGS_TAG;
[cell.contentView addSubview:feedbackRatingsView];
tapRateView = [[RSTapRateView alloc] initWithFrame:CGRectMake(feedbackRatingsView.bounds.origin.x, feedbackRatingsView.bounds.origin.y+13.0f,100.0f ,feedbackRatingsView.bounds.size.height) forStarType:kTinyStar];
tapRateView.textLabel.hidden = YES;
tapRateView.backgroundColor = [UIColor clearColor];
tapRateView.userInteractionEnabled = NO;
[feedbackRatingsView addSubview:tapRateView];
}
else{
feedbackTextLabel = (UILabel*)[cell.contentView viewWithTag:FEEDBACK_TEXT_TAG];
feedbackByUserLabel= (UILabel*)[cell.contentView viewWithTag:FEEDBACK_BY_USER_TAG];
feedbackRatingsView= (UIView*)[cell.contentView viewWithTag:FEEDBACK_RATINGS_TAG];
feedbackCountNoLabel= (UILabel*)[cell.contentView viewWithTag:FEEDBACK_COUNT_TAG];
}
[self shrinkFontSizeToFit:YES forProductCell:cell];
Feedback *feedback = nil;
if (indexPath.section == SELLER_FEEDBACK_SECTION)
{
if (indexPath.row < self.sellerFeedbacksArray.count) {
feedback = [self.sellerFeedbacksArray objectAtIndex:indexPath.row];
feedbackTextLabel.text = feedback.feedbackMessage;
feedbackByUserLabel.text = [NSString stringWithFormat:@"By %@ on %@",feedback.feedbackGivenBy,feedback.feedbackGivenOnDate];
feedbackCountNoLabel.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
tapRateView.rating = [feedback.feedbackRating intValue];
}
}
else if (indexPath.section == BUYER_FEEDBACK_SECTION)
{
if (indexPath.row < self.buyerFeedbacksArray.count) {
feedback = [self.buyerFeedbacksArray objectAtIndex:indexPath.row];
feedbackTextLabel.text = feedback.feedbackMessage;
feedbackByUserLabel.text = [NSString stringWithFormat:@"By %@ on %@",feedback.feedbackGivenBy,feedback.feedbackGivenOnDate];
feedbackCountNoLabel.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
tapRateView.rating = [feedback.feedbackRating intValue];
}
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH, 20000.0f);
CGSize size = [feedback.feedbackMessage sizeWithFont:[UIFont fontWithName:SEGOEUI size:14.0] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
[feedbackTextLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, 10, CELL_CONTENT_WIDTH , MAX(size.height, 44.0f))];
height = feedbackTextLabel.frame.size.height+feedbackTextLabel.frame.origin.y;
feedbackByUserLabel.frame = CGRectMake(120, height+5, 190, 21);
feedbackRatingsView.frame = CGRectMake(10.0, feedbackByUserLabel.frame.origin.y, 100, 30);
return cell;
}
{
UILabel *feedbackByUser= (UILabel*)[cell.contentView viewWithTag:FEEDBACK_BY_USER_TAG];
if (shouldResize) {
[self resizeFontForLabel:feedbackByUser minSize:MINIMUM_FONT_SIZE];
}
else{
feedbackByUser.adjustsFontSizeToFitWidth = NO;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
return;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Check feedback text height
Feedback *feedback = nil;
if (indexPath.section == SELLER_FEEDBACK_SECTION)
{
if (indexPath.row < [self.sellerFeedbacksArray count]) {
feedback = [self.sellerFeedbacksArray objectAtIndex:indexPath.row];
}
}
else if (indexPath.section == BUYER_FEEDBACK_SECTION)
{
if (indexPath.row < [self.buyerFeedbacksArray count]) {
feedback = [self.buyerFeedbacksArray objectAtIndex:indexPath.row];
}
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH , 20000.0f);
NSString *name = [NSString stringWithFormat:@"By %@ on %@",feedback.feedbackGivenBy,feedback.feedbackGivenOnDate];
CGSize byUserLabelSize = [name sizeWithFont:[UIFont fontWithName:SEGOEUI size:14.0] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
NSString *text = [[sellerFeedbacksArray objectAtIndex:[indexPath row]] feedbackMessage];
CGSize size = [text sizeWithFont:[UIFont fontWithName:SEGOEUI size:14.0] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2)+byUserLabelSize.height;
}
如果行高很大,则应用渐变2次。有时,滚动后某些单元格的高度也会发生变化。
有谁能告诉我这里出了什么问题?
提前致谢。