【发布时间】:2013-01-11 17:48:30
【问题描述】:
我从 .plist 填充分组表视图。我只想为每个部分放置一个复选标记。我尝试使用NSMutableArray 来保留选定的行并显示复选标记,但没有成功。
填充类:
#import <Foundation/Foundation.h>
@interface ParsePlist : NSObject
@property (nonatomic,strong) NSDictionary * agendaDic;
- (void)passStringToAgendaDic:(NSString *)string;
//survey questions
-(NSArray*)getSurvey;
-(NSString*)getSurveyQuestion:(NSInteger ) section ;
//answers
-(NSArray*)getSurveyAnswers:(NSInteger) section;
-(NSString*)getQuestionAnswer:(NSInteger ) section Row:(NSInteger) row ;
勾选代码:
- (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];
}
cell.textLabel.text = [_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]; // get string in survey
if([_selectedIndexPathArray containsObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this for trying check mark
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
//remove index path
[_selectedIndexPathArray removeObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[_selectedIndexPathArray addObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]];
}
}
在这种情况下,我如何跟踪选定的行?我应该使用NSDictionary,还是有其他方法可以做到这一点?
【问题讨论】:
-
为什么不尝试使用带有复选标记图像的按钮的自定义单元格并制作一个通用选择器。单击时将 indexpath.row 添加到数组中。这样您将选择所有索引跨度>
-
每个 UITableViewCell 都有一个 contentView,清理它并在里面放你想要的东西。
标签: iphone ios objective-c uitableview