2016-04-28 53 views
1

我試圖改變將使用的自定義單元格,這取決於存儲在我的數組中的信息。我已經嘗試了一切,但似乎無法讓它工作沒有錯誤。代碼似乎不接受單元格筆尖的字符串輸入。有沒有其他方法可以做到這一點?下面將附上我的代碼。來自NSString的UITableViewCell Nib

謝謝。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    if([[Tutortrackinbud[indexPath.row] valueForKey:@"reply"] isEqual: @"N"]) 
    { 
     nib = @"ReviewCell1"; 
    } 
    else{ 
      nib = @"ReviewCell2"; 
    } 
    static NSString *simpleTableIdentifier = nib; 



    ReviewCell1TableViewCell *cell = (ReviewCell1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 

    { 

     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:nib owner:self options:nil]; 

     cell = [nibArray objectAtIndex:0]; 

    } 
    if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"1"]){ 
     NSLog(@"1"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star3.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star4.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star5.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star6.image = [UIImage imageNamed:@"greystar.png"]; 

    } 
    else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"2"]){ 
     NSLog(@"2"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"star.png"]; 
     cell.star3.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star4.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star5.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star6.image = [UIImage imageNamed:@"greystar.png"]; 

    } 
    else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"3"]){ 
     NSLog(@"3"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"star.png"]; 
     cell.star3.image = [UIImage imageNamed:@"star.png"]; 
     cell.star4.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star5.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star6.image = [UIImage imageNamed:@"greystar.png"]; 
    } 
    else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"4"]){ 
     NSLog(@"4"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"star.png"]; 
     cell.star3.image = [UIImage imageNamed:@"star.png"]; 
     cell.star4.image = [UIImage imageNamed:@"star.png"]; 
     cell.star5.image = [UIImage imageNamed:@"greystar.png"]; 
     cell.star6.image = [UIImage imageNamed:@"greystar.png"]; 
    } 
    else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"5"]){ 
     NSLog(@"5"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"star.png"]; 
     cell.star3.image = [UIImage imageNamed:@"star.png"]; 
     cell.star4.image = [UIImage imageNamed:@"star.png"]; 
     cell.star5.image = [UIImage imageNamed:@"star.png"]; 
     cell.star6.image = [UIImage imageNamed:@"greystar.png"]; 

    } 
    else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"6"]){ 
     NSLog(@"6"); 
     cell.star1.image = [UIImage imageNamed:@"star.png"]; 
     cell.star2.image = [UIImage imageNamed:@"star.png"]; 
     cell.star3.image = [UIImage imageNamed:@"star.png"]; 
     cell.star4.image = [UIImage imageNamed:@"star.png"]; 
     cell.star5.image = [UIImage imageNamed:@"star.png"]; 
     cell.star6.image = [UIImage imageNamed:@"star.png"]; 

    } 


    cell.reviewdate.text = [Tutortrackinbud[indexPath.row] valueForKey:@"date"]; 
    cell.reviewtext.text = [NSMutableString stringWithFormat:@"%@ -%@",[Tutortrackinbud[indexPath.row] valueForKey:@"desc"],[Tutortrackinbud[indexPath.row] valueForKey:@"username"]]; 

    //cell.tutortext.text = [NSMutableString stringWithFormat:@"%@ -%@",[Tutortrackinbud[indexPath.row] valueForKey:@"desc"],[Tutortrackinbud[indexPath.row] valueForKey:@"username"]]; 







    cell.tutortext.text = @"this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test"; 






    return cell; 

} 
+0

http://stackoverflow.com/questions/14303832/uitableview-with-two-custom-cells-multiple-identifiers - might解決你的問題。 –

+0

你會得到什麼錯誤? – Lion

回答

2

你必須在你的viewDidLoad

示例代碼最好註冊您的筆尖文件:

UINib *cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil]; 
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"]; 

,然後用dequeueReusableCellWithIdentifier正常,你已經做了。您可以使用不同的標識符註冊多個nib文件,並將它們以cellForRowAtIndexPath及其唯一標識符出列。

1

我建議,而不是多個xib文件,你需要創建單個xib文件,你從NSArray得到的對象。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *simpleTableIdentifier = nib; 

    ReviewCell1TableViewCell *cell = (ReviewCell1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil){ 
     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:nib owner:self options:nil]; 

     if([[Tutortrackinbud[indexPath.row] valueForKey:@"reply"] isEqual: @"N"]){ 
       cell = [nibArray objectAtIndex:0]; 
     }else{ 
       cell = [nibArray objectAtIndex:1]; 
     } 
    } 


    return cell; 
} 
2

在我的項目,我經常做的類方法是這樣一個基本單元:

+ (NSString *)reuseIdentifier { 
    return NSStringFromClass([self class]); 
} 

+ (UINib *)nibName { 
    return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; 
} 

和所有其他細胞從它的子類,並使用這種方式它需要:

[self.tableView registerNib:[UINib nibWithNibName:[FeedCell nibName] bundle:nil] forCellReuseIdentifier:[FeedCell reuseIdentifier]]; 

希望它有幫助)