1

这是我拥有的一些工作代码,它在完成时为待办事项添加复选标记:

- (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;
}

我想要做的是删除复选标记代码和一些类似的东西(它的基本逻辑):

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

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

我还尝试更改NSStringNSAttributedStringNSMutableAttributedString基于我见过的其他一些问题和答案,例如这样的:

- (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;
}

但我不确定确切的实现,例如如何在if (toDoItem.completed)方法上调用它。这只需要iOS7支持。

项目完成后,如何在表格单元格标签上获得删除线效果?

4

2 回答 2

7

您使用的代码有几处问题。崩溃是将属性字符串设置为单元格标识符的结果。从那里开始,您应该先将单元格出列,然后再尝试为其属性分配值。您还应该从 toDoItems 数组中的对象初始化属性字符串。总的来说,我认为这就是你的意思:

- (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;
}
于 2014-04-15T19:52:44.013 回答
1
cellIdentifier.textLabel (NSAttributedString Strikethrough = NO)

如果这确实是您想要做的,请将“cellIdentifier”更改为“cell”并使用“setAttributedText”方法设置您的 NSAttributedString。单元格标识符只是一个标识符字符串,用于使单元格出列和重用。您需要在 UITableViewCell 上设置文本:

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

编辑 :

在您添加的代码中,很多地方都是错误的。首先,术语“单元标识符”通常用于描述您在出列或创建单元时放置的文本

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

其次,如果您不使用情节提要或 xib,则不会自动创建单元格。添加 :

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

在您的代码中,您在分配单元格之前设置属性字符串,然后设置文本。您需要在创建单元格后设置属性文本,并在“文本”或“属性文本”之一之间进行选择

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;
于 2014-04-15T19:47:53.817 回答