gpt4 book ai didi

ios - objective-c -完成表格单元格标签上的删除线

转载 作者:行者123 更新时间:2023-12-01 17:25:36 26 4
gpt4 key购买 nike

这是一些可用的工作代码,当完成时,会在待办事项上添加一个对勾:

- (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更改为 NSAttributedStringNSMutableAttributedString,我看到了 such as this one,如下所示:
- (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支持。

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

最佳答案

您使用的代码有几处错误。崩溃是由于将属性字符串设置为单元格的标识符而导致的。从那里开始,在尝试为单元属性分配值之前,应该先使单元出队。您还应该初始化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;
}

关于ios - objective-c -完成表格单元格标签上的删除线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23092846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com