【问题标题】:UITableView Strange Behavior while scrollingUITableView 滚动时的奇怪行为
【发布时间】:2016-01-01 14:03:54
【问题描述】:

下面是我的代码。

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 中的当前日期和日期匹配按钮出现,否则按钮保持隐藏。

【问题讨论】:

    标签: objective-c uitableview


    【解决方案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 处的按钮,因为您之前更改了该标签。

    您应该做的是创建一个UITableViewCell 的自定义子类,在其中为所有需要的界面元素创建出口,并通过出口而不是标签访问它们。创建子类后,在情节提要中将其分配给单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多