这是我拥有的一些工作代码,它在完成时为待办事项添加复选标记:
- (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;
我还尝试更改NSString
为NSAttributedString
并NSMutableAttributedString
基于我见过的其他一些问题和答案,例如这样的:
- (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支持。
项目完成后,如何在表格单元格标签上获得删除线效果?