0

UIViewControllerテーブルビューとセルを追加してセットアップしました。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = nil;
    NSString *task = [self.tasks objectAtIndex:indexPath.row];
    NSRange urgentRange = [task rangeOfString:@"URGENT"];
    if (urgentRange.location == NSNotFound) {
        identifier = @"plainCell";
    } else {
        identifier = @"attentionCell";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    // Configure the cell...

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc]
                                           initWithString:task];
    NSDictionary *urgentAttributes =
    @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24],
      NSStrokeWidthAttributeName : @3.0};
    [richTask setAttributes:urgentAttributes
                      range:urgentRange];
    cellLabel.attributedText = richTask;

    return cell;
}

ストーリーボードを学習しようとしており、このサンプル コードを作成しました。私がやっている間違いがわからない、またはそれを見つけることができます。私は昨日からこれに固執しており、問題を理解することはできません。

私の学習を進めるために、あなたの助けをお願いします。

これは私が作成しようとしている例です。XCODE プロジェクトは、次の URL からダウンロードできます。

https://dl.dropboxusercontent.com/u/72451425/Simple%20Storyboard.zip

コードを調べて、前進するのを手伝ってください。

4

2 に答える 2

3

ストーリーボードで使用するセル識別子はactionCellplainCellです。

actionCell. 違いattentionCellます。

コードで正しいセル識別子を使用してください

if (urgentRange.location == NSNotFound) {
    identifier = @"plainCell";
} else {
    identifier = @"actionCell";
}
于 2013-08-03T21:05:10.560 に答える
-3

あなたの問題はここにあります:UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

以前にセルが作成されていない場合、これは nil セルを返す可能性があります。したがって、それを確認する必要があります。

// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]]; //or do whatever makes sense for your new cell
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] initWithString:task];
NSDictionary *urgentAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24], NSStrokeWidthAttributeName : @3.0};
[richTask setAttributes:urgentAttributes range:urgentRange];
cellLabel.attributedText = richTask;
于 2013-08-03T21:06:37.967 に答える