I have a class in which I have a tableview and in the tableview I am display the values through array. When I run my application and select the cell of tableview only one particular cell gets selected at a time and its value is displayed in the detailtextlabel of my previous controller. I want that multiple selects should be enabled at a time.
Right now I am doing this. This is the class where I want that my multiple tableview cell should get selected:
TAddAlarmNewController.m
- (void)viewDidLoad {
days =[[NSMutableArray alloc]initWithObjects:@"Every Monday",@"Every Tuesday",@"Every Wednesday",@"Eve开发者_JAVA百科ry Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday",nil];
temp = [[NSDictionary alloc] initWithObjectsAndKeys:days,@"arrValue",nil];
[super viewDidLoad];
}
In the days array I am storing 7 values, and in the no of rowinsection I am counting the array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [days count];
}
and in the cellforrowatindexpath method:
- (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] autorelease];
}
cell.textLabel.text = [days objectAtIndex:indexPath.row];
cell.accessoryType = ([indexPath isEqual:rowselection]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
In this I am adding my arrays to the textlabel of my cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//rowselection is an indexpath variable.
self.rowselection = indexPath;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
for (int i=0; i<[[self.navigationController viewControllers] count]; i++)
{
UIViewController *aController = [[self.navigationController viewControllers]objectAtIndex:i];
if ([aController isKindOfClass:[TAddAlarmController class]])
{
TAddAlarmController *addalarm = (TAddAlarmController*)aController;
newrepeat =(NSString*)[[days objectAtIndex:indexPath.row] retain];
[addalarm refreshTableToSetDetailText:newrepeat];
}
}
//TAddAlarmController is the previous controller in which detailtextlabel i am setting the next controller value.
}
The link provided by Vijay solves ur problem. Also u need to keep an array (with bool values) to maintain whether a row is selected or not. So while reloading the table view, u can put the checkmarks correctly.
You should try this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath.row;
CustomViewCell* theCell = (CustomViewCell *)[tableView cellForRowAtIndexPath:indexPath];
theCell.mySelectedImage.hidden = NO;
//[tableView reloadData];
[selectedIndices insertObject:[NSNumber numberWithInt:indexPath.row] atIndex:arrayIndex]; //the selectedIndices is array that stores index of selected rows
NSLog(@"%@",[selectedIndices objectAtIndex:arrayIndex]);
arrayIndex++;
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomViewCell *cell = (CustomViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[CustomViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.mySelectedImage.hidden = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// this loop will check for selected index when table gets reload
for (int i = 0 ; i < [selectedIndices count]; i++)
{
NSLog(@"%@",[selectedIndices objectAtIndex:i]);
int t = indexPath.row;
int ind = [[selectedIndices objectAtIndex:i] intValue];
if (t == ind)
{
cell.mySelectedImage.hidden = NO;
}
}
return cell;
}
精彩评论