添加/编辑/移动自定义 TableViewCell?

Posted

技术标签:

【中文标题】添加/编辑/移动自定义 TableViewCell?【英文标题】:Add/Edit/Move Custom TableViewCell? 【发布时间】:2013-02-26 08:22:52 【问题描述】:

**.h

IBOutlet UITableView *Table;
NSMutableArray *Currency;
NSArray *Datacell;

** 创建自定义数组

Custom *usd = [Custom new];
usd.name = @"USD";
usd.detail = @"United States Dollar";
usd.imageFile = @"usd.jpg";


Custom *eur = [Custom new];
eur.name = @"EUR";
eur.detail = @"Euro Member Countries";
eur.imageFile = @"eur.jpg";

Custom *aud = [Custom new];
aud.name = @"AUD";
aud.detail = @"Australia Dollar";
aud.imageFile = @"AUD.jpg";
Currency = [NSArray arrayWithObjects:usd,eur,aud, nil];

覆盖以支持编辑表格视图。**

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 
 if (editingStyle == UITableViewCellEditingStyleDelete) 
// Delete the row from the data source

     [Currency removeObjectAtIndex:indexPath.row];
     [Table reloadData];
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 
 else if (editingStyle == UITableViewCellEditingStyleInsert) 
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 
 

崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x682a0c0'

【问题讨论】:

【参考方案1】:

显然数组货币没有 [Currency removeObjectAtIndex:indexPath.row] 指向的元素;

您已将货币定义为 NSMutableArray 并使用 Currency = [NSArray arrayWithObjects:usd,eur,aud, nil]; 初始化;

将其设为 [NSMutableArray arrayWithObjects:usd,eur,aud, nil];

【讨论】:

【参考方案2】:

您正在将Currency 对象初始化为NSArray

替换

Currency = [NSArray arrayWithObjects:usd,eur,aud, nil];

Currency = [NSMutableArray arrayWithCapacity:3];
[Currency addObject:usd];
[Currency addObject:eur];
[Currency addObject:aud];

【讨论】:

以上是关于添加/编辑/移动自定义 TableViewCell?的主要内容,如果未能解决你的问题,请参考以下文章

添加了 IBOutlet 和自定义 TableViewCell 不再起作用

以编程方式将 UILabel 添加到自定义 tableViewCell

编辑完成后如何从自定义uitableviewcell的文本字段向数组添加数据

IOS Swift 5 为动态高度 TableView 单元添加自定义 TableViewCell 分隔符

ios 7 - 自定义 UIMenuItem 在 TableViewCell 上不起作用

如果 TableViewCell 有大量数据要显示,则在 UITableViewCell 中添加自定义按钮的最佳方法。使用斯威夫特