嗨,在我的应用程序中,我在 UITableView
中显示本地数据库 sqlite
中的数据。在该数据中,我的 UILabel
中显示了整数值。现在想要通过单击按钮来increment
和decrement
我尝试过的值,但它不能正常工作,当再次单击下一行时它只能工作一次它显示旧值请告诉我如何解决这个问题。我为递增和递减动态创建了两个 UIButtons
。
我的数据库数据获取代码。
qtt=[[NSMutableArray alloc]init];
[self openDB];
NSString *sql =[NSString stringWithFormat"SELECT * FROM br"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement)== SQLITE_ROW) {
char *field5 = (char *)sqlite3_column_text(statement, 4);
NSString *field5Str =[[NSString alloc]initWithUTF8String:field5];
str2 = [[NSString alloc]initWithFormat"%@",field5Str];
[qtt addObject:str2];
}
}
我的 UITableView
显示代码。
-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{
static NSString *cellIdentifier =@"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle"+" forState:UIControlStateNormal];
[button addTarget:self actionselector(aMethod
forControlEvents:UIControlEventTouchUpInside];
button.tag=indexPath.row;
[cell.contentView addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle"-" forState:UIControlStateNormal];
[button1 addTarget:self actionselector(aMethod1
forControlEvents:UIControlEventTouchUpInside];
button1.tag=indexPath.row;
[cell.contentView addSubview:button1];
if([valueString isEqualToString:[self.qtt objectAtIndex:indexPath.row]])
{
int value=[valueString integerValue];
if (self.check==YES)
{
value=value+1;
str2=[NSString stringWithFormat"%d",value];
NSLog(@"%@",str2);
}
else
{
if (value==1)
{
}
else
{
value=value-1;
str2=[NSString stringWithFormat"%d",value];
NSLog(@"%@",str2);
}
}
cell.ttt.text=str2;
}
return cell;
}
我的UIButton
Action 方法代码。
-(void)aMethodid)sender
{
self.check=YES;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData];
}
-(void)aMethod1id)sender
{
self.check=NO;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData]:
}
问题在于,每次通过 tableView:cellForRowAtIndexPath:
代码时,您都会不断创建按钮。当您第一次调用 reloadData
时,有足够的单元格可供重用,因此您的代码在旧按钮之上添加了新按钮;每次单击 [+]
或 [-]
时都会发生这种情况。
要解决此问题,请根据条件创建按钮。为您的按钮提供不同的标签 - 例如,[+]
为 100,[-]
为 101。这将让您通过标签找到 subview ,并避免在它们已经存在时创建新按钮。由于您需要在 aMethod:
和 aMethod1:
中使用该标签,请将 indexPath.row
的标签放在单元格本身上,然后使用 int tag = [sender.superview tag]
表达式来检索它。
以下是您如何实现此功能的框架:
-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{
static NSString *cellIdentifier =@"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
if ([cell.contentView viewWithTag:100] == nil) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle"+" forState:UIControlStateNormal];
[button addTarget:self actionselector(aMethod
forControlEvents:UIControlEventTouchUpInside];
button.tag=100;
[cell.contentView addSubview:button];
}
if ([cell.contentView viewWithTag:101] == nil) {
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle:@"-" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(aMethod1
forControlEvents:UIControlEventTouchUpInside];
button1.tag=101;
[cell.contentView addSubview:button1];
}
cell.contentView.tag = indexPath.row;
// Remove the increment/decrement code, it does not belong here
}
您的事件处理程序方法如下所示:
-(void)aMethodid)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]+1;
[self.qtt replaceObjectAtIndex:tag withObject:[@(value) stringValue]];
[self.mytableview reloadData];
}
-(void)aMethod1id)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]-1;
if (value <= 0) value = 1;
[self.qtt replaceObjectAtIndex:tag withObject:[@(value) stringValue]];
[self.mytableview reloadData];
}
关于ios - 如何通过单击 UIButton IOS7 更新 UILabel 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24385436/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |