Reputation:
I am developing and tableView in which in each row I have to display the click counter and row number.Each cell must have an initial click counter value of 0 when application is initialized. Then increment the click counter value within a cell whenever a user clicks on the cell.
I have take 26 fixed rows. I have taken tableData.plist file as in the attached image.
I am initializing the self.dataArray with the plist .
Now I want to do implementation in the didSelectRowAtIndexPath delegate method,if any row is tapped that row's click counter should increment.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"tableData" ofType:@"plist"];
self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];//array initialized with plist
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"MyCellID";
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(160.0f, 2.0f, 30.0f, 20.0f)];//label for rowid
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(160.0f, 24.0f, 30.0f, 30.0f)];//label for counter
label2.textColor = [UIColor grayColor];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCustomCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if([self.dataArray count]>indexPath.row)
{
label1.text = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"Lable1"];
label2.text =[[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"Lable2"];
}
else {
label1.text = @"";
label2.text =@"";
}
[cell.contentView addSubview:label1];
[cell.contentView addSubview:label2];
[label1 release];
[label2 release];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//implementation for row counter incrementer.
}
Upvotes: 0
Views: 1387
Reputation: 3346
Try this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//take dictionary object from dataArray at selected index
//take Lable2 value as a string and convert it into integer and increment the integer value
//now convert this incremented integer value into string
//now save it new string in Lable2 for the object dictionary and add this dict object into dataArray at selected index
NSMutableDictionary * tempDict = [self.dataArray objectAtIndex:indexPath.row];
int tempLableInt = [[tempDict objectForKey:@"Lable2"] intValue];
tempLableInt = tempLableInt +1;
[tempDict setObject:[NSString stringWithFormat:@"%d",tempLableInt] forKey:@"Lable2"];
[self.dataArray replaceObjectAtIndex:indexPath.row withObject:tempDict];
[tableView reloadData];
}
Upvotes: 1
Reputation: 21221
You could do the following define this property in your class
NSMutableDictionary *dicCounter;
Then in your didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"%@,%@", indexPath.section, indexPath.row];
//Get the old counter
int counterForCell = [[dicCounter valueForKey:cellIdentifier] intValue];
//increment it
counterForCell++;
//set it in the dictionary
[dicCounter setValue:[NSNumber numberWithInt:counterForCell] forKey:cellIdentifier];
}
Upvotes: 0
Reputation: 2481
just set nsdictionary value.In that set forkey is your uitableview didselectrowatindexpath value.Based on the for key value increment the value.
Upvotes: 0