Pregunta

Here is some working code that I have which adds a checkmark to a to-do item when it is complete:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {
         cell.accessoryType = UITableViewCellAccessoryNone;
    }    return cell;
}

What I want to do is remove the checkmark code and some something like (basic logic of it):

    if (toDoItem.completed) {
        cellIdentifier.textLabel (NSAttributedString Strikethrough = YES)

    } else {
         cellIdentifier.textLabel (NSAttributedString Strikethrough = NO)
    }    return cell;

I've also tried to change NSString to NSAttributedString and NSMutableAttributedString based on some other questions and answers I've seen such as this one like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary* attributes = @{
                             NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
                             };
    static NSAttributedString* cellIdentifier = [[NSAttributedString alloc] initWithString:@"ListPrototypeCell" attributes:attributes];
    cell.textLabel.attributedText = cellIdentifier;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;


    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }    return cell;
}

But I'm not sure of the exact implementation, for example how to call this on the if (toDoItem.completed) method. This will only need to be supported by iOS7.

How can I get the strikethrough effect on my Table Cell Label when the item is complete?

¿Fue útil?

Solución

There are several things wrong with the code you're using. The crash is the result of setting an attributed string as the cell's identifier. From there, you should be dequeuing the cell before you try to assign values to its properties. You should also be initializing the attributed string from the object in your toDoItems array. Overall, I think this is what you meant to do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];


    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];

    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        NSDictionary* attributes = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]};
        NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:toDoItem.itemName attributes:attributes];

        cell.textLabel.attributedText = attributedString;

    } else {
        cell.textLabel.text = toDoItem.itemName;
        cell.accessoryType = UITableViewCellAccessoryNone;
    } 

    return cell;
}

Otros consejos

cellIdentifier.textLabel (NSAttributedString Strikethrough = NO)

If this is really what you are trying to do, change "cellIdentifier" to "cell" an use the method "setAttributedText" to set your NSAttributedString.The cell identifier is only an identifier string that is used to dequeue and reuse cells. You need to set the text on the UITableViewCell :

NSAttributedString *attributedString; 
//Format your string
[cell.textLabel setAttributedText:attributedString];

EDIT :

In the code you added, many things are wrong. First, the term "cell identifier" is normally use to describe the text you would put when dequeuing or creating a cell

NSString *cellIdentifier = @"ListPrototypeCell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

second, if you're not using storyboard, or xibs, the cells are not created automatically. add :

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

In your code, you set the attributed string before the cell is assignated, and then set the text. You need to set your attributed text after cell creation and choose between just one of "text" or "attributedText"

NSString *cellIdentifier = @"ListPrototypeCell";
    NSDictionary* attributes = @{
                                 NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
                                 };

    NSAttributedString* attributedText = [[NSAttributedString alloc] initWithString: @"The striked text" attributes:attributes];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];

    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.text = nil;
        cell.textLabel.attributedText = attributedText;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = toDoItem.itemName;
        cell.textLabel.attributedText = nil;
    }
    return cell;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top