我想在表格中为每个表格行添加一个复选框(或者如果复选框是一个更好的名称)。一个简单的小框,用于标记是否读取了一行。只是为了记住。当我关闭应用程序时,当然要保存。如果应用程序更新,也仍然保持不变。
有没有简单的方法可以做到这一点?
周围有样品吗?
在下面的代码中,我使用了一个代码来制作待办事项列表,这里我使用了两个NSMutableArray
itsToDoTitle和itsToDoChecked来填充表格行..我希望这会对你有所帮助..
您可以拥有itsToDoTitle和itsToDoChecked数组,NSUserDefaults
或者您可以写入一些属性文件,以便再次拥有相同的列表。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"ToDoList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [itsToDoTitle objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
BOOL checked = [[itsToDoChecked objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
button.tag = indexPath.row;
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
return cell;
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
BOOL checked = [[itsToDoChecked objectAtIndex:indexPath.row] boolValue];
[itsToDoChecked removeObjectAtIndex:indexPath.row];
[itsToDoChecked insertObject:(checked) ? @"FALSE":@"TRUE" atIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Save"
style:UIBarButtonItemStylePlain target:self action:@selector(saveChecklist:)];
self.navigationItem.rightBarButtonItem = backButton;
[backButton release];
}