adding check box to one of sections in UITableView

2019-09-12 11:03发布

问题:

I create a table view controller programmatically that contains different sections I want to add 3 rows with check mark box in my third sections (I used storyboard!) would you please give me some hint that how can I do that ..

my question is how can I set checkbox in left side for my third sections

here is the picture:instead of Please set your code: having 3 rows with check mark box in Absence Code sections

Here is my view in storyboard:

Here is the code:

- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];


NSString *staKey = @"Start";
NSString *endKey = @"End";
NSString *absKey= @"Absence";

[contents setObject:[NSArray arrayWithObjects:@"Time: 08:00 Date: Fri,3 Aug, 2012", nil] forKey:staKey];
[contents setObject:[NSArray arrayWithObjects:@"Time: 17:57 Date: Fri,3 Aug, 2012", nil] forKey:endKey];
[contents setObject:[NSArray arrayWithObjects:@"Please set your code", nil] forKey:absKey];

[keys addObject:staKey];
[keys addObject:endKey];
[keys addObject:absKey];


[self setSectionKeys:keys];
[self setSectionContents:contents];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier];
}

[[cell textLabel] setText:contentForThisRow];
return cell;

 }


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSMutableArray *weekArray = [[ NSMutableArray alloc] initWithObjects: @"Start Time", @"End Time",@"Absence Code", 
 nil];

return [weekArray objectAtIndex:section];
}


- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath  
*)indexPath {
return UITableViewCellAccessoryDisclosureIndicator;
}

Thanks in advance!

回答1:

Each checkbox cell should be a Custom TableViewCell. Then simply drop an ImageView and a Label in the TableViewCell and use didSelectRowAtIndex: to toggle the image between checked/unchecked.



回答2:

EDIT:

Check Selecting multiple rows of a UITableView link get helped.

Do this: change code in below method:

- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath  *)indexPath {
 if(indexPath.section ==2)
 {
  return UITableViewCellAccessoryCheckMark;
 }
 else
 {
  return UITableViewCellAccessoryDisclosureIndicator;
 }
}