Ios NSString中的UITableViewCell Nib

Ios NSString中的UITableViewCell Nib,ios,objective-c,uitableview,nsarray,custom-cell,Ios,Objective C,Uitableview,Nsarray,Custom Cell,我正试图根据数组中存储的信息更改将使用的自定义单元格。我已经尝试了所有的方法,但似乎无法让它正常工作。代码似乎不接受单元格nib的字符串输入。还有别的办法吗?将在下面附上我的代码 谢谢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if([[Tutortrackinbud[indexPath.row] valueForKey

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

}

您最好在
viewDidLoad

示例代码:

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

然后使用
dequeueReusableCellWithIdentifier
,就像您已经做的那样。您可以注册多个具有不同标识符的
nib
文件,只需在
cellforrowatinexpath
中将它们以其唯一标识符排在队列中即可。

您最好在
viewDidLoad
中注册nib文件

示例代码:

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

然后使用
dequeueReusableCellWithIdentifier
,就像您已经做的那样。您可以使用不同的标识符注册多个
nib
文件,只需在
cellForRowAtIndexPath
中使用其唯一标识符将它们排在队列中。

我建议,而不是多个
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;
}

我建议,而不是多个
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;
}

在我的项目中,我经常使用如下类方法执行基本单元:

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

(希望有帮助)

在我的项目中,我经常使用如下类方法执行基本单元:

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

希望有帮助)

-可能会解决您的问题。您遇到了什么错误?-可能会解决您的问题。您遇到了什么错误?