在我的tableview中我有4个部分,我需要在每个部分有1个复选标记。像这样......
等..
numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numberOfRows;
if (section == 0) {
numberOfRows = 3;
}
else if (section == 1) {
numberOfRows = 4;
}
else if (section == 2) {
numberOfRows = 4;
}
else if (section == 3) {
numberOfRows = 3;
}
return numberOfRows;
}
的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *userChoices = @"UserChoices";
cell = [tableView dequeueReusableCellWithIdentifier:userChoices];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userChoices];
if (indexPath.section == 0) {
cell.textLabel.text = [mail objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
cell.textLabel.text = [search objectAtIndex:indexPath.row];
}
else if (indexPath.section == 2) {
cell.textLabel.text = [social objectAtIndex:indexPath.row];
}
else if (indexPath.section == 3) {
cell.textLabel.text = [maps objectAtIndex:indexPath.row];
}
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
didSelectRowAtIndexPath方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
if(self.checkedIndexPath)
{
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
// use this to put checkmark on the item
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
以下代码在表的所有部分的单元格旁边给了一个复选标记。任何机构都有任何想法吗?
答案 0 :(得分:0)
创建NSNumbers的NSMutableArray,其中包含由数组中的元素表示的部分中所选元素的整数表示。
您必须进行更多设置,但检查时
if([self.checkedIndexPath isEqual:indexPath])
你可以检查
if([[yourArrayOfCheckedIndexes objectAtIndex:indexPath.section] integerValue] == indexPath.row)
基本上你需要存储每个部分的检查索引
答案 1 :(得分:0)
您可以使用字典来跟踪每个部分上检查的行:
NSMutableDictionary *_checkedRowsDict = [NSMutableDictionary dictionaryWithCapacity:numberOfSections];
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([_checkedRowsDict objectForKey:[NSNumber numberWithInt:indexPath.section]]) {
// Unmark the row found on _checkedRowsDict
NSNumber *num = [_checkedRowsDict objectForKey:[NSNumber numberWithInt:indexPath.section]];
int row = [num intValue];
NSIndexPath *checkIndexPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:checkIndexPath];
checkedCell.accessoryType = UITableViewCellAccessoryNone;
}
// Mark the selected row
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
// Update _checkedRowsDict
[_checkedRowsDict setObject:[NSNumber numberWithInt:indexPath.row] forKey:[NSNumber numberWithInt:indexPath.section]];
...
}
类似于初始化tableView:cellForRowAtIndexPath:
答案 2 :(得分:0)
试试这个,对我来说工作得很好:
- 变量:
@property NSInteger checkedCellRow;
@property NSInteger checkedCellSection;
-functions:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkedCellSection == indexPath.section)
{
if (checkedCellRow == indexPath.row)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
checkedCellRow = indexPath.row;
checkedCellSection = indexPath.section;
[myTable reloadData];
}
答案 3 :(得分:-1)
我将它添加到我的viewDidLoad中,一切正常。 THX。
-(void)viewDidLoad {
NSUInteger i = 2; //Number of section
NSMutableDictionary *_checkedRowsDict = [NSMutableDictionary dictionaryWithCapacity:i];
...
}