2016-01-01 131 views
1

以下是我的代码。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]; 
} 

虽然滚动tableview“如果条件”确实满足,但按钮不隐藏。我想要做的是,如果当前日期和日期在API匹配按钮出现其他按钮仍然隐藏。

回答

1

下面的代码将导致重大问题:

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

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

,对于使用电池在第一时间就开始运行,因为你resuse细胞,标签将不再匹配,你不会检索实际的按钮。如果创建第一个单元格,则将checkinBtn的标记设置为0.将该单元格从屏幕上滚动并重新使用后,将不再能够找到索引9或10处的按钮,因为您之前改变了这个标签。

你应该做的,而不是做标签魔术是创建一个自定义的子类UITableViewCell在那里你创建网点所有需要的界面元素和访问他们通过插座而不是标签。创建子类后,将其分配到故事板中的单元格中。