i am Newbie in iOS Development, i make an App that Contain UITableView I want to Set a Tag to UITableViewCell like as if it Was Selected then Cell tag is 1 else cell tag is 0 for that i Do Coding like as But it was always Set tag is equal 1 i want if cell was selected then tag is 1 else tag is equal to 0 Please Give me Solution for it.
Here my Code like as
in viewDidLoad
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]];
SelectedRows is NSMutableArray
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tag=1;
intValue=cell.tag;
NSLog(@"Tag %d",intValue);
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.tag=0;
NSLog(@"Tag %d",cell.tag);
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Selected Cell Value %@",selectedCell);
NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
[SelectedRows removeObject:obj];
[tableView reloadData];
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[SelectedRows addObject:obj];
[tableView reloadData];
}
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:SelectedRows forKey:@"SelectedRows"];
[userDef synchronize];
}
And i Pass this Tag Value in Url Like as
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/insert_gcm.php?gcm_id=%@& category1=%d&category2=%d&category3=%d&category4=%d&category5 =%d&category6=%d&category7 =%d&category8=%d& category9=%d",uniqueIdentifier,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue]];
Here i want tag Value 1 if cell was Selected and tag Value 0 if cell is not selected Please Give me Solution for it
The problem is that you're setting intValue to 1 in your cellForRowAtIndexPath: method:
if ([SelectedRows containsObject:obj])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tag=1;
intValue=cell.tag;
NSLog(@"Tag %d",intValue);
}
That's why intValue always equals 1.
And the solution is NOT to set the intValue equal to 0 in the other conditional of cellForRowAtIndexPath:. Although you're correctly setting your selected cells' tags to 1 and your unselected cells' tags to 0 as you intended, you shouldn't assign your intValue variable in this method. Instead, you should set your intValue depending on the current tag. For example, if there's a save button in each row, you'll want to set that button's tag to 0 or 1 then utilize the parameter in the the button press method to set intValue equal to sender.tag.
Edit: As was made clear in the comments "intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue" used in forming the url are actually supposed to be different numbers, each one representing a different cell's tag. But by using intValue repeatedly like that, you're actually using the same exact value 9 times. You actually need to use 9 different values to do what you're attempting to do.
You can create an array of those 9 values dynamically in cellForRowAtIndexPath:.
In viewDidLoad you can initialize a class NSMutableArray variable with exactly 9 indexes all containing 0:
self.tagArray = [[NSMutableArray alloc] init];
for (int i = 0 ; i < 9 ; i ++) {
[self.tagArray addObject:[NSNumber numberWithInt:0]];
}
Then update those indices in cellForRowAtIndexPath::
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tag=1;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.tag=0;
}
[self.tagArray replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInteger:cell.tag]];
return cell;
}
Then during perform save, use the tags stored in your tag array:
- (void)performSave {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/insert_gcm.php?gcm_id=%@&category1=%@&category2=%@&category3=%@&category4=%@&category5=%@&category6=%@&category7=%@&category8=%@&category9=%@", uniqueIdentifier, self.tagArray[0], self.tagArray[1], self.tagArray[2], self.tagArray[3], self.tagArray[4], self.tagArray[5], self.tagArray[6], self.tagArray[7], self.tagArray[8]]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With