滚动时UITableView奇怪的行为

时间:2016-01-01 14:03:54

标签: objective-c uitableview

以下是我的代码。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo" forIndexPath:indexPath]; 
UILabel * name = (UILabel *)[cell viewWithTag:4];
UILabel * age = (UILabel *)[cell viewWithTag:5];
UILabel * city = (UILabel *)[cell viewWithTag:6];

NSString * currentDate = self.calenderDates[indexPath.section];
NSArray * dicDate = [self.dataDictionay valueForKey:currentDate];

UIButton * checkinBtn = (UIButton*)[cell viewWithTag:9];
UIButton * checkoutBtn = (UIButton*)[cell viewWithTag:10];

checkinBtn.tag = indexPath.row;
checkoutBtn.tag = indexPath.row;

NSString * papikDate = [dicDate[indexPath.row] valueForKey:@"strat_date"];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString * todayDate = [dateFormat stringFromDate:[NSDate date]];

if ([papikDate isEqualToString:todayDate]) {
    [checkinBtn setHidden:NO];
    [checkinBtn setHidden:NO];
} else {  
    [checkinBtn setHidden:YES];
    [checkinBtn setHidden:YES];
}

滚动桌面视图"如果条件"确实满足,但按钮不隐藏。我想要做的是,如果API匹配按钮中的当前日期和日期按钮出现,则按钮保持隐藏状态。

1 个答案:

答案 0 :(得分:1)

以下代码会导致重大问题:

UIButton * checkinBtn = (UIButton*)[cell viewWithTag:9];
UIButton * checkoutBtn = (UIButton*)[cell viewWithTag:10];

checkinBtn.tag = indexPath.row;
checkoutBtn.tag = indexPath.row;

第一次使用单元格时,只要重新单元格,标签将不再匹配,您将无法检索实际按钮。如果您创建了第一个单元格,则将checkinBtn的标记设置为0.在屏幕上滚动该单元格并重新使用后,您将无法再在索引9处找到该按钮或10,因为您之前更改过该标记。

你应该做什么而不是做tag-magic就是创建一个UITableViewCell的自定义子类,你可以为所有需要的界面元素创建出口,并通过出口而不是标签来访问它们。创建子类后,将其在故事板中分配给单元格。

相关问题