这是我的代码,可以正确处理UITableview Cell的多个选择。
现在,我的问题是在这里我创建了两个数组,其中一个存储数组数据,另一个存储选定的数据。但是,我想只使用一个数组。有人给了我解决方案,可以通过提供密钥使用KVC(valueForKeyPath)来完成。但我不知道该怎么做。
如果有人知道请帮助我。
#import "NewTableViewController.h"
@implementation NewTableViewController
@synthesize attTableData ;
@synthesize arrSelectionData ;
- (void)viewDidLoad
{
[super viewDidLoad];
self.attTableData=[NSArray arrayWithObjects:@"Dhosa",@"Kahman",@"Dhokla",@"Handvo",@"chodafadi",@"Muthiya",@"Medu Vada",@"Patra",@"Thepla",@"Fafda",@"Gota",@"gathiya",@"Idali",@"Dalvada",@"Samosa",nil];
self.arrSelectionData=[NSMutableArray array];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.attTableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if([self.arrSelectionData containsObject:indexPath])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.attTableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arrSelectionData containsObject:indexPath])
{
[self.arrSelectionData removeObject:indexPath];
}
else
{
[self.arrSelectionData addObject:indexPath];
}
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//[tableView reloadData];
}
@end
解决方案::
“配方列表”文件中的数据
[0]{
Item = Thepla;
selected = 0;
},
[1]
{
Item = Gota;
selected = 0;
},
[2]
{
Item = Handvo;
selected = 0;
},
[3]
{
Item = Idali;
selected = 0;
},
[4]
{
Item = Dalvada;
selected = 0;
},
现在我的TableViewController
#import "NewTableViewController.h"
#import "TableViewCell.h"
@implementation NewTableViewController
@synthesize attTableData ;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Recipe List" ofType:@"plist"];
attTableData = [[NSMutableArray alloc]initWithContentsOfFile:path];
attTableData = (NSMutableArray *)CFBridgingRelease(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)attTableData, kCFPropertyListMutableContainers));
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.attTableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"UserCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
NSDictionary * dicTemp = [attTableData objectAtIndex:indexPath.row];
// NSNumber* attendingObject = [dicTemp objectForKey:@"selected"];
if([dicTemp objectForKey:@"selected"] == [NSNumber numberWithBool:NO])
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
cell.lbl.text=[[self.attTableData objectAtIndex:indexPath.row]objectForKey:@"Item"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableDictionary * dic = [attTableData objectAtIndex:indexPath.row];
//NSNumber* attendingObject = [dic objectForKey:@"selected"];
if ([dic objectForKey:@"selected"] == [NSNumber numberWithBool:NO])
{
[dic setValue:[NSNumber numberWithBool:YES] forKey:@"selected"];
}
else
{
[dic setValue:[NSNumber numberWithBool:NO] forKey:@"selected"];
}
[attTableData replaceObjectAtIndex:indexPath.row withObject:dic];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end
答案 0 :(得分:0)
如果你想使用单个数组,而不是像这样做
self.attTableData=[NSArray arrayWithObjects:@"Dhosa",@"Kahman",@"Dhokla",@"Handvo",@"chodafadi",@"Muthiya",@"Medu Vada",@"Patra",@"Thepla",@"Fafda",@"Gota",@"gathiya",@"Idali",@"Dalvada",@"Samosa",nil];
为您的商品创建模型类
//pseudo code
class Item{
name = "Dhosa"
selected = false
}
所以attTableData
将是
self.attTableData=[Item1, Item2];
现在,当您进行选择时,您可以设置项目的selected
属性。在cellForRow
中,选中此属性并设置setAccessoryType
。