【问题标题】:Titles and NSLog not running in cellForRowAtIndexPath标题和 NSLog 未在 cellForRowAtIndexPath 中运行
【发布时间】:2015-01-23 19:23:08
【问题描述】:

我似乎无法理解为什么我不能显示单元格的标题和副标题。我已经尝试通过NSLog() 获取字符串对象,但除了NSLog() 之外,我没有收到任何输出,它成功打印了数组@987654324 中测试对象(Stack 对象)的内存位置@。

这是我的cellForRowAtIndexPath: 方法:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell" forIndexPath:indexPath];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SimpleCell"];
    }

    NSString *cell_Title = [[array_Stacks objectAtIndex:[indexPath row]] title];
    NSString *cell_Subtitle = [[array_Stacks objectAtIndex:[indexPath row]] subject];

    NSLog(@"%@", cell_Title);

    NSLog(@"%@", [array_Stacks objectAtIndex:[indexPath row]]);
    NSLog(@"%@", cell_Subtitle);

    cell.textLabel.text = cell_Title;
    cell.textLabel.tintColor = [cp color_master_tan];
    cell.detailTextLabel.text = cell_Subtitle;
    cell.detailTextLabel.tintColor = [cp color_master_tan];
    //cell.imageView.image = [UIImage imageNamed:@"asset_Cell"];

    return cell;
}

注意:“cp”是我的调色板对象,它返回一个UIColor 对象。

这里是 Stack.m

#import "Stack.h"
#import "FlashCard.h"

/*
 Notes

 * Need to get the name of the creator and add it to the default init.

 */

@implementation Stack
{
    NSMutableArray *array_flashCard;
}
@synthesize title, subject, creator, array_FlashCard;

#pragma mark - Initializers

- (id)init {
    return [self initWithTitle:@"New Stack" subject:@"No Subject" creator:@"None Listed" array:array_FlashCard];
}

- (id)initWithTitle:(NSString *) ttl subject:(NSString *) sbj creator:(NSString *) ctr array:(NSMutableArray *) arr {
    self = [super init];
    if (self) {
        title = ttl;
        subject = sbj;
        creator = ctr;
        array_FlashCard = arr;

        title = [[NSString alloc] init];
        subject = [[NSString alloc] init];
        creator = [[NSString alloc] init];
        array_FlashCard = [[NSMutableArray alloc] init];

    }
    return self;
}

#pragma mark - Array Operations

- (void)addFlashCardToArray:(FlashCard *) flashcard { [array_FlashCard addObject:flashcard]; }

@end

【问题讨论】:

  • 字符串为空。
  • 首先,它被调用了吗?设置一个断点,看看它是否被触发。如果没有,您将什么设置为表格视图的数据源?您的方法是否被调用,表明表中有多少行?如果是,它会返回什么?
  • 第二个 NSLog 打印出正确的信息,即数组中 Stack 对象的内存地址,所以我知道该对象存在并且该方法正在被触发。文件的所有者已设置。问题是,即使字符串为空,我至少会在终端中得到带有时间戳的“nil”,但我什么也得不到。
  • 不,字符串是有效的对象,但它们是空的。 nil 与空字符串不同,NSLog() 将不会打印任何内容,如果您提供的只是一个空字符串。

标签: ios objective-c cocoa-touch nslog


【解决方案1】:

您在Stack initWithTitle:... 中将字符串设置为@""

    title = ttl;
    subject = sbj;
    creator = ctr;
    array_FlashCard = arr;

    title = [[NSString alloc] init]; // Throw away previous value; assign new empty string.
    subject = [[NSString alloc] init]; // Throw away previous value; assign new empty string.
    creator = [[NSString alloc] init]; // Throw away previous value; assign new empty string.
    array_FlashCard = [[NSMutableArray alloc] init]; // Throw away previous value; assign new empty array.

【讨论】:

  • 做到了。我仍然不确定为什么我没有得到其他两个 NSLog 的时间戳为零,但现在我的标题出现了。谢谢!
  • 同样,@Cole,字符串是有效对象,而不是nil。空字符串与nil不同。
【解决方案2】:

您必须明白,在大多数情况下,这只是一个小错误。因此,您必须检查以下内容。

  1. 代理和数据源是否设置正确。并设置断点是否正确调用委托方法。
  2. 数据源是否不返回零对象。因为如果数据源不包含任何对象,cellForRowAtIndexPath 将不会被调用。

希望这会有所帮助,相信我,这并不严重。你可能只是错过了一些东西。

奇怪的是你已经分配了titlesubjectcreatorarray_FlashCard。但是您再次对其进行初始化,因此它们不再为空。

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多