gpt4 book ai didi

iphone - iOS:点击 TableView 中行的计数器

转载 作者:行者123 更新时间:2023-11-29 04:35:55 25 4
gpt4 key购买 nike

我正在开发 tableView,其中每行都必须显示点击计数器和行号。初始化应用程序时,每个单元格的初始点击计数器值必须为 0。然后,每当用户单击单元格时,就增加单元格内的点击计数器值。

我有 26 个固定行。我已获取如附图所示的 tableData.plist 文件。 enter image description here

我正在使用 plist 初始化 self.dataArray 。

现在我想在 didSelectRowAtIndexPath 委托(delegate)方法中实现,如果点击任何行,该行的点击计数器应该增加。

- (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.
}

最佳答案

试试这个。

- (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];
}

关于iphone - iOS:点击 TableView 中行的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com