ios - 来自 NSString 的 UITableViewCell Nib

标签 ios objective-c uitableview nsarray custom-cell

我正在尝试根据存储在我的数组中的信息更改将使用的自定义单元格。我已经尝试了一切,但似乎无法让它在没有错误的情况下正常工作。该代码似乎不接受单元格 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;

}

最佳答案

在我的项目中,我经常用这样的类方法做一个基本单元:

+ (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]];

希望能帮助到你)

关于ios - 来自 NSString 的 UITableViewCell Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912026/

相关文章:

iphone - body 和面部检测问题

ios - 是否可以创建一个包含 4 个部分的 uitableview,但第一部分没有单元格

ios - 在没有 iOS 13 的情况下使用 Xcode 在 iOS 13 设备上运行

ios - 自定义图像滤镜和多种彩色滤镜

iphone - 使用 UISwipeGestureRecognizer 滑动 View

objective-c - 基于 View 的NSTableView过滤+动画

ios - 为tableview中的不同文本字段重用相同的pickerview

ios - 删除所选行 - 不是最后保存的行

ios - 将音频样本从流附加到AVAssetWriter

ios - SKStoreProductViewController 显示来自开发者的应用程序