[英]UITableView iOS
我有一個包含5個部分的表格視圖,每個部分僅包含1行,每行顯示一個自定義單元格。 一切工作正常,但第4部分重復了第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];
}
對不同的單元格使用不同的單元格標識符。 當新單元格滾動到您的視圖中時,從您的視圖中滾動出來的舊單元格將根據您在出隊時提供的標識符進行重用。
讓我猜,在加載時,您的第五個單元格不在您的視野范圍內?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.