UITableView iOS



我有一个包含 5 个部分的表格视图,每个部分仅包含 1 行,每行显示一个自定义单元格。 一切正常,但第 0 节在第四节重复,我无法追踪这一点。这是我的代码。

-(void) setUpActivityView {
    [self createViews];
}

#pragma  mark Table View methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) { 
        return 167;
    }
    else if (indexPath.section == 1) {
        return 200;
    }
    else if (indexPath.section == 2) {
        return 115;
    }
    else if (indexPath.section == 3) {
        return 90;
    }
    else {
        return 155;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"customCellIdentifier";
    if (indexPath.section == 0 && indexPath.row == 0) {
        _notificationCell = (NotificationsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (_notificationCell == nil) {
            _notificationCell = [[[NotificationsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        _notificationCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _notificationCell;
    }
    else if (indexPath.section == 1 && indexPath.row == 0) {
         _topMatchCell = (TopMatchCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (_topMatchCell == nil) {
            _topMatchCell = [[[TopMatchCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
        }
        _topMatchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _topMatchCell;
    }
    else if (indexPath.section == 2 && indexPath.row == 0) {
        _recentlyViewedCell = (RecentlyViewedHomesCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (_recentlyViewedCell == nil) {
            _recentlyViewedCell = [[[RecentlyViewedHomesCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        _recentlyViewedCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _recentlyViewedCell;
    } else if (indexPath.section == 3 && indexPath.row == 0) {
        _feedbackCell = (FeedbackCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (_feedbackCell == nil) {
            _feedbackCell = [[[FeedbackCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        _feedbackCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _feedbackCell;
    }
    else {
        _activityTimeLineCell = (ActivityTimeLineCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (_activityTimeLineCell == nil) {
            _activityTimeLineCell = [[[ActivityTimeLineCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        _activityTimeLineCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _activityTimeLineCell;
   }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)initializeSubViews {
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 412) style:UITableViewStylePlain];
}

- (void)configureLayoutOfSubViews {
    self._mainTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_Setup1-bg.png"]];
//    UIColor *lSeperatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"line_bg.png"]];
//    [self._mainTableView setSeparatorColor:lSeperatorColor];
}

- (void)setStylesForSubViews {
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTableView.showsVerticalScrollIndicator = NO;
    _mainTableView.scrollsToTop = NO;
}
- (void)setStateForSubViews {
}
- (void)registerTargets {
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
}
-(void)addToParentsView {
     [self addSubview:_mainTableView];
}
-(void) createViews {
    [self initializeSubViews];
    [self configureLayoutOfSubViews];
    [self setStylesForSubViews];
    [self setStateForSubViews];
    [self registerTargets];
    [self addToParentsView];
}

对不同的单元格使用不同的单元格标识符。当新单元格滚动到视图中时,将根据您在取消排队时提供的标识符重复使用从视图中滚动出来的旧单元格。

让我猜猜,加载时您的第五个单元格不在您的视野中?

最新更新