2012-05-30 178 views
1

我想在表格中爲每個表格行添加一個複選框(或者如果複選框是一個更好的名稱)。 一個小的簡單框,用於標記一行是否被讀取。只是要記住。 當然,當我關閉應用程序時會被保存。 而且如果應用程序更新仍然不變。表格中的複選框

有沒有簡單的方法來做到這一點?
周圍有沒有樣品?

+0

[你有什麼試過?](http://whathaveyoutried.com) –

回答

3

在下面的代碼我使用一個代碼,使待辦事項列表,我在這裏使用兩個NSMutableArrayitsToDoTitleitsToDoChecked填充表rows..I希望這將幫助你..

你可以擁有的itsToDoTitleNSUserDefaults itsToDoChecked陣列,也可以在一些屬性文件中寫,這樣你將再次有相同的列表..

- (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]; 

} 
+0

謝謝,會試試這個。我相信它會解決我的任務。 –