自定义UITableViewCell元素不仅仅出现在iPad上

时间:2015-01-26 18:01:53

标签: ios objective-c ipad uitableview

我在iPad上运行应用程序时遇到了以下问题。我的自定义第一行UITableViewCell的UI元素没有出现。我手动分配和定位这些元素,因此问题与使用错误的xib无关。我已经上传了在iPhone 6iPad 2上运行完全相同的代码的结果。请注意iPad上第一行单元格是空的,但正确地有两个UITextField元素和iPhone 6上的UIButton

经过一些实验,我知道现在导致这个问题的原因,但我不知道为什么会这样。这是我对tableView:cellForRowAtIndexPath:

的实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *summonerCellIdentifier = @"summonerCellIdentifier";
    static NSString *footerCellIdentifier = @"footerCellIdentifier";
    static NSString *headerCellIdentifier = @"headerCellIdentifier";

    UITableViewCell *cell;
    if (indexPath.row == 0) {
        // header view 1
        cell = [tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
        if (cell == nil) {
            cell = [UITableViewCell new];
        }
        cell.backgroundColor = [UIColor LHBeigeLight];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        self.summonerName = [[UITextField alloc] initWithFrame:CGRectMake(0,30,200,40)];
        [self.summonerName setCenter:CGPointMake(self.view.center.x, self.summonerName.center.y)];
        [self.summonerName setBackgroundColor:[UIColor whiteColor]];
        [self.summonerName setLeftViewMode:UITextFieldViewModeAlways];
        [self.summonerName setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]];
        [self.summonerName setPlaceholder:@"Summoner Name"];
        [self.summonerName setKeyboardType:UIKeyboardTypeEmailAddress];
        [self.summonerName setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [self.summonerName setAutocorrectionType:UITextAutocorrectionTypeNo];
        // [self.summonerName setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
        [cell.contentView addSubview:self.summonerName];

        self.summonerRegion = [[UITextField alloc] initWithFrame:CGRectMake(0,80,200,40)];
        [self.summonerRegion setCenter:CGPointMake(self.view.center.x, self.summonerRegion.center.y)];
        [self.summonerRegion setBackgroundColor:[UIColor whiteColor]];
        [self.summonerRegion setLeftViewMode:UITextFieldViewModeAlways];
        [self.summonerRegion setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]];
        [self.summonerRegion setPlaceholder:@"Summoner Region"];
        [self.summonerRegion setKeyboardType:UIKeyboardTypeEmailAddress];
        [self.summonerRegion setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [self.summonerRegion setAutocorrectionType:UITextAutocorrectionTypeNo];
        // [self.summonerRegion setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
        [cell.contentView addSubview:self.summonerRegion];

        self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0,130,200,40)];
        [self.searchButton setCenter:CGPointMake(self.view.center.x, self.searchButton.center.y)];
        [self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
        [self.searchButton.titleLabel setTextColor:[UIColor whiteColor]];
        [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldColor]] forState:UIControlStateNormal];
        [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldLightColor]] forState:UIControlStateHighlighted];
        [self.searchButton setBackgroundImage: [AppDelegate imageFromColor:[UIColor LHGoldLightColor]] forState:UIControlStateSelected];
        [self.searchButton setContentEdgeInsets:UIEdgeInsetsMake(5, 15, 5, 15)];
        [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
        // [self.searchButton setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
        [cell.contentView addSubview:self.searchButton];

        _pickerView = [[UIPickerView alloc] init];
        [self.summonerName setDelegate:self];
        [self.summonerRegion setInputView:_pickerView];
        [self.summonerRegion setDelegate:self];

        // Initialize Data
        _pickerData = @[@"NA", @"EUW", @"EUNE", @"BR", @"KR", @"LAN", @"LAS", @"OCE", @"RU", @"TR"];

        // Connect data
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.showsSelectionIndicator = YES;

        [self.summonerRegion setText:[_pickerData objectAtIndex:0]];
    } else if (indexPath.row == (recentSearchesArray.count+1)) {
        // footer view 1
        cell = [tableView dequeueReusableCellWithIdentifier:footerCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:footerCellIdentifier];
        }
        cell.frame = CGRectMake(0, 0, self.recentSearchesTable.frame.size.width, 140);
        cell.backgroundColor = [UIColor LHRedColor];
        cell.textLabel.textColor = [UIColor LHBeigeLight];
        cell.textLabel.text = @"Follow Us On Twitter";
        cell.detailTextLabel.textColor = [UIColor LHBeigeLight];
        cell.detailTextLabel.text = @"for app updates and events!";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else if (indexPath.row == (recentSearchesArray.count+2)) {
        // footer view 2
        cell = [tableView dequeueReusableCellWithIdentifier:footerCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:footerCellIdentifier];
        }
        cell.frame = CGRectMake(0, 0, self.recentSearchesTable.frame.size.width, 140);
        cell.textLabel.textColor = [UIColor LHBeigeLight];
        cell.backgroundColor = [UIColor LHGreenColor];
        cell.textLabel.text = @"Battlefy Tournaments";
        cell.detailTextLabel.textColor = [UIColor LHBeigeLight];
        cell.detailTextLabel.text = @"Check out our community tournaments!";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        // regular cell
        Summoner *summoner = [recentSearchesArray objectAtIndex:indexPath.row-1];

        cell = [tableView dequeueReusableCellWithIdentifier:summonerCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:summonerCellIdentifier];
        }
        cell.textLabel.text = summoner.summonerName;
        cell.detailTextLabel.text = [summoner.summonerRegion uppercaseString];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    return cell;
}

我注释掉并取消格式化了在第一个单元格中破坏自定义视图的三行。无论出于何种原因,如果我设置左右柔性自动调整蒙版,则单元格元素会在iPad版本上消失。我使用这些UIViewAutoresizing蒙版来处理旋转(通过使元素在第一个单元格中保持水平居中)。

有没有人知道为什么会这样?如果我设置这个0索引UITableViewCell的方式不好或者我的代码有其他问题请告诉我!


修改:更多信息。这就是tableView:heightForRowAtIndexPath:的样子:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 200;
    } else if (indexPath.row >= recentSearchesArray.count + 1) {
        return 130;
    } else {
        return 44;
    }
}

这些是iPhone 6 po [self.view recursiveDescription]的输出:

(lldb) po [self.view recursiveDescription]
<UIView: 0x7ff3e156d290; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3e156d360>>
   | <UITableView: 0x7ff3e1858e00; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ff3e156ad60>; layer = <CALayer: 0x7ff3e1568610>; contentOffset: {0, 0}; contentSize: {320, 0}>
   |    | <UITableViewWrapperView: 0x7ff3e156bbf0; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x7ff3e156c660>; layer = <CALayer: 0x7ff3e156c160>; contentOffset: {0, 0}; contentSize: {320, 568}>
   |    | <UIImageView: 0x7ff3e15698b0; frame = (0 565.5; 320 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3e15294a0>>
   |    | <UIImageView: 0x7ff3e156a5d0; frame = (317.5 561; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3e156a5b0>>

和iPad 2:

(lldb) po [self.view recursiveDescription]
<UIView: 0x7869a230; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x786a2b20>>
   | <UITableView: 0x78907600; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x78690b00>; layer = <CALayer: 0x786a62c0>; contentOffset: {0, 0}; contentSize: {320, 0}>
   |    | <UITableViewWrapperView: 0x786a9470; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x786a8290>; layer = <CALayer: 0x7866ab40>; contentOffset: {0, 0}; contentSize: {320, 568}>
   |    | <UIImageView: 0x786a9160; frame = (0 565; 320 3); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7866a750>>
   |    | <UIImageView: 0x78679fe0; frame = (317 561; 3 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x78671490>>

编辑2:添加了最佳实践的出列代码


编辑3:根据Brian的建议切换到更好的单元格初始化函数[UITableViewCell new];,并将子视图添加到内容视图而不是单元格本身。仍然没有运气。


编辑4 FIXED:最后,我不得不切换到使用自定义UITableViewCell子类并直接在.xib中执行所有约束来解决此问题。我不确定底层原因是什么,但如果你仍然想要重现它,你可以这样做,只需让测试应用程序中的每个单元格成为我为0索引创建的单元格。


编辑5 FIXED X 2:我找到了上述代码原始问题的解决方案。您可以在此处查看https://stackoverflow.com/a/28185714/740474

1 个答案:

答案 0 :(得分:1)

我回去再次看问题,最后找到了真正的罪魁祸首。我试图将子视图水平居中在单元格中,因此调用的内容如下:

[self.summonerName setCenter:CGPointMake(self.view.center.x, self.summonerName.center.y)];

无论出于何种原因self.view.center.x评估iPad上完全出乎意料的事情,并将内容视图放在屏幕外。我真正应该做的事情(以及修复上述代码的问题)是基于单元格中心设置子视图中心而不是 superview&#39; s 像这样的中心:

[self.summonerName setCenter:CGPointMake(cell.center.x, self.summonerName.center.y)];

为了向任何好奇的人展示,我创建了一个绝对的准系统示例项目,以说明您可以在此处查看的此问题: https://github.com/oseparovic/ipaduitableviewbug