UitableView의 2 가지 유형의 사용자 정의 UitableViewCells
-
05-07-2019 - |
문제
내 uitableview에서 RSS의 첫 번째 뉴스를 설정하고 싶습니다. 사용자 정의 TableViewCell (유형 A가 말하면)과 다른 뉴스 두 번째, 세 번째 등을 피드하고 싶습니다. Trype A) 첫 번째 뉴스를 위해 생성 된 것은 재사용되지만 호기심으로 CustomViewCell (A 형)의 첫 번째 사용과 동일한 유형의 CustomViewCell의 두 번째 모양이 동일하지 않습니다.
내 CellforseAtIndexPath는 이렇게 보입니다.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
static NSString *CellIdentifier = @"Cell";
if(feedIndex == 0){
MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
else{
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
return nil;
}
두 가지 유형의 세포는 높이가 다르며 올바르게 설정됩니다. 누군가가 첫 번째 뉴스 (재사용되지 않음)에 대해서만 나타나도록 사용자 정의 셀을 만드는 방법에 대한 올바른 방향으로 나를 지적 할 수 있습니까? 감사합니다
해결책
두 가지 스타일의 셀에 대한 다른 셀 식별자를 만들어야합니다.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
if(feedIndex == 0) {
MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
else {
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
[[[cell subviews] objectAtIndex:0] setTag:111];
}
cell.feed = item;
return cell;
}
return nil;
}
다른 팁
나는 당신의 질문을 완전히 이해하지 못하지만 두 가지 호기심 많은 것을 발견했습니다. 두 가지 다른 셀 유형을 사용하는 경우 'dequeeerusablecellwithidentifier'를 호출 할 때 두 개의 별개의 셀 식별자를 사용해야합니다. 현재 두 가지 모두에 동일한 식별자를 사용하고 있는데, 이는 잘못된 것입니다. 다음과 같은 것을 시도하십시오.
static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";
또한 나는 다음과 같은 것을 본 적이 없습니다.
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
왜 사용하지 않는가 :
int feedIndex = indexPath.row;
CellforseAtindexPath에서
if ("Condition for cell 1") {
cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"];
if (cellV == nil) {
[[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil];
cellV = "OUTLET-CEll-IN-VC";
}
} else {
cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"];
if (cellV == nil) {
[[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil];
cellV = "OUTLET-CEll-IN-VC";
}
}
[self configureCell:cellV indexpath:indexPath withClipVo:clip];
return cellV;
제휴하지 않습니다 StackOverflow