IOS UITableViewCells all jumbled in first cell, Iphone5S device only though, 64-bit simulator

StackOverflow https://stackoverflow.com/questions/23570068

  •  19-07-2023
  •  | 
  •  

質問

I've got a UITableView that is behaving oddly. Specifically, it has a constant 8 cells, generated in cellForRowAtIndexPath, with some basic text and images. In the simulator it works great. On my Iphone5 it works identical to the simulator, however on my Iphone5s, the cells are all jumbled on top of each other where the first cell should exist. The images below show the 5s, and the simulator/5 respectively. The left "log out of the app" button should exist in the last cell, and is being added on top of other content, noted by the arrow also.

EDIT: The 3rd image is the 64-bit simulator, with an updated cellForRowAtIndexPath that is super-simplified to just set the textLabel to "Hello" for all 8 rows.

Autolayout is not being used anywhere in the app.

The Iphone 5S is running IOS7.1.1, but the Iphone5 is running IOS7.1. The simulator is part of latest Xcode (5.1.1) and comes with most recent 7.1. I can't imagine the latest .1 for the 5s could be causing this.

Any thoughts why this is jumbled, and specifically why only on the 5S?

iphone5s iphone5 or simulator 64bit

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

- (float)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    float height =  self.view.frame.size.height / [self tableView:tableView numberOfRowsInSection:indexPath.section];
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"CellMain";

    UITableViewCell *_cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (_cell == nil) {
        _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *user_fullname = [userDefaults valueForKey:@"user_fullname"];

    _cell.textLabel.textColor = [UIColor colorWithHexString:@"58A5D5"];
    _cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"58A5D5"];
    if(indexPath.row == 0){
        _cell.textLabel.text = [NSString stringWithFormat:@"%@",@"User Name"];//user_fullname];
        _cell.detailTextLabel.text = [NSString stringWithFormat:@"Member since: %@", @"April 28, 2014"];
        _cell.imageView.image = [UIImage imageNamed:@"profile_photo.png"];
        _cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

        UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right.png"]];
        arrowImg.frame = CGRectMake(292, 14, 18, 30);
        [_cell.contentView addSubview:arrowImg];
    } else if(indexPath.row == 1){
        _cell.textLabel.text = [NSString stringWithFormat:@"ActivitySync"];
        _cell.detailTextLabel.text = [NSString stringWithFormat:@"Last sync: %@", @"5 minutes ago"];
        _cell.imageView.image = [UIImage imageNamed:@"iphone_sm.png"];
        _cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
    } else if(indexPath.row == 2){
        _cell.contentView.backgroundColor = [UIColor colorWithHexString:@"D9DBDB"];
    } else if(indexPath.row == 3){
        _cell.textLabel.text = [NSString stringWithFormat:@"%@",@"Challenges"];
        UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right.png"]];
        arrowImg.frame = CGRectMake(292, 14, 18, 30);
        [_cell.contentView addSubview:arrowImg];
    } else if(indexPath.row == 4){
        _cell.textLabel.text = [NSString stringWithFormat:@"%@",@"Setup a New Device"];
        UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right.png"]];
        arrowImg.frame = CGRectMake(292, 14, 18, 30);
        [_cell.contentView addSubview:arrowImg];
    } else if(indexPath.row == 5){
        _cell.textLabel.text = [NSString stringWithFormat:@"%@",@"Help"];
        UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right.png"]];
        arrowImg.frame = CGRectMake(292, 14, 18, 30);
        [_cell.contentView addSubview:arrowImg];
    } else if(indexPath.row == 6){
        _cell.contentView.backgroundColor = [UIColor colorWithHexString:@"D9DBDB"];
    } else if(indexPath.row == 7){
        UIButton *logout = [[UIButton alloc] initWithFrame:CGRectMake(10, 9, 300, 39)];
        [logout setTitle:@"Log Out of App" forState:UIControlStateNormal];
        logout.titleLabel.textColor = [UIColor colorWithHexString:@"58A5D5"];
        [logout addTarget:self action:@selector(logoutBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_cell.contentView addSubview:logout];
    }

    return _cell;
}
役に立ちましたか?

解決

Return CGFloat, not float from your heightForRow method. This is a 64-bit issue - the types are defined differently there. I think returning a float is causing the heights to be treated as zero in 64 bit devices.

You need to change the return type of the method, and also the declaration inside it. Don't use float for UIKit stuff, it's always going to be CGFloat.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top