我正在开发和tableView,其中在每行中我必须显示点击计数器和行号。初始化应用程序时,每个单元格的初始点击计数器值必须为0。然后,只要用户单击单元格,就会在单元格内增加单击计数器值。
我有26个固定行。我已经获取了tableData.plist文件,如附图所示。
我正在使用plist初始化self.dataArray。
现在我想在didSelectRowAtIndexPath委托方法中执行,如果点击任何行,那么行的点击计数器应该递增。
- (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.
}
答案 0 :(得分:1)
试试这个。
- (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];
}
答案 1 :(得分:0)
只需设置nsdictionary值。在那个设置forkey是你的uitableview didselectrowatindexpath值。基于for键值增加值。
答案 2 :(得分:0)
您可以执行以下操作 在您的类中定义此属性
NSMutableDictionary *dicCounter;
然后在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];
}