我是objective-c编码的新手。我试图在表视图控制器上实现一个复选标记。我希望复选标记在我单击某个单元格时显示,并在我再次单击该单元格时消失。
例如,我在下面找到了这个代码。但有一件事我不太清楚。"data"one_answers"checkData"是什么意思?我们必须在那个代码之前设置它们吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// do usual stuff here including getting the cell
// determine the data from the IndexPath.row
if (data == self.checkedData)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// determine the selected data from the IndexPath.row
if (data != self.checkedData) {
self.checkedData = data;
}
[tableView reloadData]; }
所以我在搜索一些代码,但我找不到任何我想要的代码。
如果有任何帮助,我都会很高兴的。
谢谢。
您发布的代码不适合一般用途。要支持多个单元格选择,您需要一个额外的数据结构(例如布尔值数组或NSIndexSet
)来跟踪所选索引。然后,一旦获得tableView:didSelectRowAtindexPath:
,就更新数据结构并强制重新加载表,即[self.tableView reloadData]
,以显示/隐藏选择复选标记
一些代码块
@implementation TableViewController {
NSArray * _data;
NSMutableIndexSet *_selectedIndexes;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
...
self.selectedIndexes = [[NSMutableIndexSet alloc] init];
...
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([_selectedIndexes containsIndex:indexPath.row]) {
[_selectedIndexes removeIndex:indexPath.row];
} else {
[_selectedIndexes addIndex:indexPath.row];
}
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
if ([_selectedIndexes containsIndex:indexPath.row])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
...
return cell;
}
请注意,NSMutableIdexSet
的长度必须与NSArray
的长度相同,表示用于填充tableView单元格的数据结构。或者,正如我所说,您可以使用BOOL数组。
创建一个包含所选索引的属性:
@property (nonatomic, assign) NSInteger selectedIndex;
然后:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndex == indexPath.row) {
self.selectedIndex = -1;
} else {
self.selectedIndex = indexPath.row;
}
[tableView reloadData];
}
并显示复选标记:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// do usual stuff here including getting the cell
if (indexPath.row == self.selectedIndex) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
有几种方法可以支持表单元格上的复选标记。这个示例代码似乎假设您的表视图中只有一个选中的单元格,点击另一个单元格会将复选标记移动到另一个单元。
基本上,checkedData
将是类的一个属性(通常您将使用UITableViewController
的子类,但它不需要是,只要您的类同时符合UITableViewDataSource
和UITableViewDelegate
)。data
变量源自所选行。
以下是填补空白的一种方法:
YourClass.h
// ...
@property int checkedIndex;
// ...
YourClass.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do usual stuff here including getting the cell
// See if this row is the checked row
int thisRowIndex = indexPath.row;
if (thisRowIndex == self.checkedIndex)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// determine the selected data from the IndexPath.row
int thisRowIndex = indexPath.row;
if (thisRowIndex != self.checkedData) {
self.checkedIndex = thisRowIndex;
}
[tableView reloadData];
}
当然,这并不能真正解决你的问题。由于您希望单元格进行切换,因此需要记录哪些单元格处于打开状态,哪些单元格处于关闭状态。如果您只切换一个或两个单元格,则可以在类中使用一个或多个BOOL
属性来表示每个单元格的状态。如果要切换的单元格数量较多,则应将每个单元格的状态存储在NSMutableArray
中,并获取/设置与每个索引匹配的值。
YourClass.h
// ...
{
NSMutableArray *_checkedRows; // This is an instance variable, not a property.
}
// ...
YourClass.m
// ================================================================
// Don't forget to initialize _checkedRows in your [init] method!!!
// ================================================================
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do usual stuff here including getting the cell
// See if this row is the checked row
BOOL rowIsChecked = [[_checkedRows objectAtIndex:indexPath.row] boolValue];
if (rowIsChecked)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// determine the selected data from the IndexPath.row
// Set the state of the cell to the opposite of what it currently is.
BOOL rowIsChecked = [[_checkedRows objectAtIndex:indexPath.row] boolValue];
[_checkedRows replaceObjectAtIndex:indexPath.row
withObject:[NSNumber numberWithBool:!rowIsChecked]];
[tableView reloadData];
}