iOS:UITableView reloaddata不会重新加载所有单元格

时间:2015-04-25 02:13:07

标签: ios objective-c iphone uitableview ios8

这是我的场景,我的View Controller在模态segue之后接收数据,我在viewWillAppear中调用reloaddata方法。我的意思是创建一个最多只显示4行的tableView。第一行显示相同的内容。如果数据更多4,表View只显示前两个数据,最后一行显示是否有更多数据。如果数据小于4,表View只显示第一行加数据行。 这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_assmebleModel.cityId.count < 4) {
        return _assmebleModel.cityId.count + 1;
    }else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"CityBarCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        if (0 == indexPath.row) {
            UIImageView *markerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Map Marker"]];
            markerImageView.frame = CGRectMake(20, 5, 30, 30);

            UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 15, 80, 20)];
            cityLabel.textColor = [UIColor whiteColor];
            cityLabel.backgroundColor = [UIColor clearColor];
            cityLabel.text = @"编辑地点";
            [cell addSubview:markerImageView];
            [cell addSubview:cityLabel];
        }
        if (0 != indexPath.row && _assmebleModel.cityId.count < 4) {
            UIImageView *markerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Marker"]];
            markerImageView.frame = CGRectMake(20, 5, 30, 30);

            UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 15, 80, 20)];
            cityLabel.textColor = [UIColor whiteColor];
            cityLabel.backgroundColor = [UIColor clearColor];
            WeatherInfoModel *infoModel = _assmebleModel.cityId[indexPath.row - 1];
            cityLabel.text = infoModel.cityName;
            [cell addSubview:markerImageView];
            [cell addSubview:cityLabel];
        }
        if (0 != indexPath.row && 3 != indexPath.row && _assmebleModel.cityId.count >= 4) {
            UIImageView *markerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Marker"]];
            markerImageView.frame = CGRectMake(20, 5, 30, 30);

            UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 15, 80, 20)];
            cityLabel.textColor = [UIColor whiteColor];
            cityLabel.backgroundColor = [UIColor clearColor];
            WeatherInfoModel *infoModel = _assmebleModel.cityId[indexPath.row - 1];
            cityLabel.text = infoModel.cityName;
            [cell addSubview:markerImageView];
            [cell addSubview:cityLabel];
        }
        if (3 == indexPath.row && _assmebleModel.cityId.count >= 4) {
            UIImageView *markerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"More"]];
            markerImageView.frame = CGRectMake(20, 5, 30, 30);
            UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Expand Arrow"]];
            arrowImageView.frame = CGRectMake(110, 5, 30, 30);

            UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 15, 80, 20)];
            cityLabel.textColor = [UIColor whiteColor];
            cityLabel.backgroundColor = [UIColor clearColor];
            cityLabel.text = [NSString stringWithFormat:@"显示其他%lu个项目",(unsigned long)(_assmebleModel.cityId.count - 3)];
            [cell addSubview:arrowImageView];
            [cell addSubview:markerImageView];
            [cell addSubview:cityLabel];
        }
我一次添加一个数据,每次添加数据时,都会调用viewDidAppear。我认为tableView更新每个单元格的原因我称之为reloaddata。但是看起来没有,看来reloaddata只更新了添加新数据。当我的数据超过4时,程序将执行第二个If,我的最后一个单元格显示不正确。

我很困惑,请有人帮助我,谢谢你们。

0 个答案:

没有答案