2009-09-10 33 views
52

trong UITableView của tôi tôi muốn đặt cho các tin tức đầu tiên của một nguồn cấp dữ liệu rss một tableViewCell tùy chỉnh (Loại A cho phép nói) và cho các tin tức thứ hai, thứ ba vv. trype B) vấn đề là tùy chỉnh tableViewCell (trype A) tạo ra cho những tin tức đầu tiên được tái sử dụng, nhưng tò mò số lượng hàng giữa việc sử dụng đầu tiên của customViewCell (loại A) và sự xuất hiện thứ hai của cùng một loại customViewCell là không bằng nhau ..2 loại khác nhau của UITableViewCells tùy chỉnh trong UITableView

cellForRowAtIndexPath của tôi trông giống như thế này.

- (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; 
}  

hai loại ô có độ cao khác nhau được đặt chính xác. ai đó có thể chỉ cho tôi đi đúng hướng trên làm thế nào để làm cho một loại tế bào tùy chỉnh để xuất hiện chỉ cho những tin tức đầu tiên (không được tái sử dụng)? cảm ơn bạn

+0

cho Phiên bản Swift của câu hỏi này xem [tại đây] (http://stackoverflow.com/ câu hỏi/30774671/uitableview-với-nhiều hơn một-tùy chỉnh-tế bào-với-swift) – Honey

Trả lời

78

Bạn nên tạo một nhận dạng tế bào khác nhau cho hai phong cách của tế bào:

- (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; 
} 
+0

cảm ơn bạn nó đã làm việc. Bạn có biết nếu có thể thiết lập các customcell khác nhau cho các phần khác nhau của một tableView? phần 0 sẽ có loại A và phần 1 sẽ có loại B .. Cảm ơn bạn –

+0

tìm thấy câu trả lời .. (NSIndexPath *) indexPath có thuộc tính hàng cũng là thuộc tính phần hữu ích :) Sorin –

+0

int feedIndex = indexPath.row; - là đủ tốt ở dòng đầu tiên –

8

tôi không hoàn toàn hiểu câu hỏi của bạn, nhưng nhận thấy hai điều tò mò. Nếu bạn đang sử dụng hai loại ô khác nhau, bạn cần phải sử dụng hai số nhận dạng ô riêng biệt khi gọi 'dequeueReusableCellWithIdentifier'. Bạn hiện đang sử dụng cùng một số nhận dạng cho cả hai, không chính xác. Hãy thử một cái gì đó như:

static NSString *MainArticleIdentifier = @"MainArticle"; 
static NSString *NewsIdentifier = @"News"; 

Ngoài ra, tôi chưa bao giờ thấy bất cứ điều gì như:

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1]; 

Tại sao không chỉ cần sử dụng:

int feedIndex = indexPath.row; 
0

trong cellForRowAtIndexPath

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; 
Các vấn đề liên quan