Working with some of UITableViewCell - ios

How to select several UITableViewCell (as a checkmark but with my own style) and then do something with selected items?
As a checkmark (or something else) I would like to use UIImageView:

To add to Pandu1251's answer, if you want to use your own check mark image, you can set a custom accessory view and follow the same logic that he has specified.
To set a custom accessory view, use
cell.accessoryView = myAccessoryView; //myAccessoryView is a UIImageView holding your custom check mark image

-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
for(int i=0;i<[Array count];i++)
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
Now try this......

Just do as follow. For that you have to take NSMutableArray. i have taken arrMethodsToBeStored here.
- (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 setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.textColor=[UIColor darkGrayColor];
cell.textLabel.font= [UIFont fontWithName:#"Arial Rounded MT Bold" size:15.0];
Scope_RepairMethod *scpObj=[repairMethodArr objectAtIndex:indexPath.row];
cell.textLabel.text= scpObj.strRepairMethod;
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSString *selID;
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
Scope_RepairMethod *scpObj=[repairMethodArr objectAtIndex:indexPath.row];
if (newCell.accessoryType == UITableViewCellAccessoryNone)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
[arrMethodsToBeStored addObject:scpObj.strRepairMethod];
}
else
{
newCell.accessoryType = UITableViewCellAccessoryNone;
[arrMethodsToBeStored removeObject:scpObj.strRepairMethod];
}
NSLog(#"selected method arr==== %#",arrMethodsToBeStored);
// I want to enable my save button only if one or more values from table are selected
if(arrMethodsToBeStored.count > 0)
saveBtn.enabled=true;
else
{
saveBtn.enabled=false;
}
}

Related

When i scrolling on UITableview,the checkmark of multiple selected rows are disappears.how to solve this?

I want to implement multiple row selection in UITableview.but when i select multiple row with checkmark and scroll down,the checkmarks are disappears when i come back to selected rows section.I have tried many solutions from Stackoverflow but it doesn't work for me.Can you please anyone give me the solution which works for me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([_selectedstatearray containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
NSLog(#"cellText>>%#",cellText);
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[_selectedstatearray removeObject:cellText];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[_selectedstatearray addObject:cellText];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
NSLog(#"selectedarray>>%#",_selectedstatearray);
NSString *greeting = [_selectedstatearray componentsJoinedByString:#","];
NSLog(#"%#",greeting);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
The problem lies in this piece of code:
if([_selectedstatearray containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You are checking if _selectedstatearray contains the specified indexPath while you store them as NSString [_selectedstatearray addObject:cellText];
Replace the code above with this:
NSString *text = [statearray objectAtIndex:indexPath.row];
if([_selectedstatearray containsObject:text]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
Answer for your last comment
If you are passing selectedstatearray to another ViewController's (viewControllerB) UITableView which will contain only selectedstatearray then you can just add
cell.accessoryType = UITableViewCellAccessoryCheckmark
At your UITableVIew delegate method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Which will add check-mark to all row's
Hope it helps....

Adding disclosure indicator to tableview cell

I believe I'm doing this correctly but it doesn't seem to be working? The text for the cell is written indicating the if statement is correct and is running, However the cell accessory is not changing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.section==0) {
if ([[tableContents objectAtIndex:indexPath.row] isEqual: #"New Time"]) {
cell.tintColor = [UIColor redColor];
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
return cell;
}
Thanks for any help in advance!
Okay so tried slimming the code down to try and see whats going on, now this crashes when the view is loaded, totally confused as i have another project where this is working perfectly!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"UserCells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = #"Hello";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Setting the cell tint color in cellForRowAtIndexPath did work:
cell.tintColor = [UIColor redColor]; // whatever you want

UITableViewCellSelectionStyleBlue is not working

I need to use UITableViewCellSelectionStyleBlue for the cell selection style. But whatever style I set it only displays UITableViewCellSelectionStylegray. I am using Xcode 5.
This is my code:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = #"Table1";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==Nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.selectionStyle =UITableViewCellSelectionStyleBlue;
cell.imageView.image =[UIImage imageNamed:#"cellbgimg.png"] ;
return cell;
}
i am able to change the UITableviewSelectionStyleBlue. Please try below code:
// take a view for the cell...
UIView *cellBg = [[UIView alloc] init];
cellBg.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // this RGB value for blue color
cellBg.layer.masksToBounds = YES;
Cell.selectedBackgroundView = cellBg;
Try this one ....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifier:#"myCell" ForIndexpath:indexPath];
}
[self configureCell:cell forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; // This one for your cell selection style.
return cell;
}

First check mark hides when i started scrolling table

- (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];
}
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
this is my code when ever i started scrolling the table towards downwards or upwards the first cell which i have checked have loosed the check mark sign, it is just hides all the way.. what is wrong with my code?
Reuse Cell ,
- (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];
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
} // END HERE
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
For selecting multiple rows:
- (void)addTableView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.timeSheetEntryTableView.allowsMultipleSelection = YES;
self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];
}
//Cell for row at index path
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"Cell Identifier";
self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!self.cell) {
self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
self.cell.accessoryType = UITableViewCellAccessoryNone;
}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box_selected.png"]];
}
else
{
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box.png"]];
}
return self.cell;
}
//did select and deselect
// I have used custom cell
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box_selected.png"]];
}
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array removeObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box.png"]];
}

Multiple checking at one time accesoryType

So, there is a problem: when i checking a cell in table, another cell checked also, but i want only one checking at time. There is source:
- (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 = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:#"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(indexPath);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Try to not reuse the cell.
Because when you reuse the cell you also reuse the checkmark-/accessory-state.
So why not removing the cell queuing.
You can try:
.h file:
#property (nonatomic, retain) NSMutableDictrionary *checkedState;
.m file:
(at top)
#synthesize checkedState = checkedState_;
(rest)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil] autorelease];
cell.textLabel.text = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:#"name"];
if(self.checkedState) {
NSNumber *checkmarkStateNumber = [self.checkedState objectForKey:[NSNumber numberWithInt:indexPath.row]];
if(checkmarkStateNumber && [checkmarkStateNumber boolValue] == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(indexPath);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(!checkedState) { self.checkedState = [NSMutableDictionary dictionary]; }
BOOL checkmarkState = NO;
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
checkmarkState = YES;
}
[self.checkedState setObject:[NSNumber numberWithBool:checkmarkState] forKey:[NSNumber numberWithInt:indexPath.row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Update 1:
Now it should also store the checkmark state!
Add the following property to your table view controller:
#property(nonatomic, retain) NSIndexPath* selectedIndexPath;
Synthesize the property in your table view controller's implementation:
#synthesize selectedIndexPath;
Add this line to dealloc:
self.selectedIndexPath = nil;
Add this to tableView:cellForRowAtIndexPath::
cell.accessoryType == (indexPath.section == self.selectedIndexPath.section && indexPath.row == self.selectedIndexPath.row) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
Add this to tableView:didSelectRowAtIndexPath::
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
self.selectedIndexPath = nil;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndexPath = indexPath;
}
The advantages are:
you can still reuse cells;
even if the view gets unloaded as a result of a low memory warning, it will restore the checkmark after the table view is reloaded.

Resources