ios - 在 cellForRowAtIndexPath 中设置 UILabel 的计数

标签 ios iphone objective-c

我陷入了一个奇怪的场景。我正在制作一个购物车屏幕,我有 UITableView 并且每一行都包含一个加号按钮、一个减号按钮和一个标签,其值会根据用户按下的按钮而增加和减少.现在,当我按下第 1 行的添加按钮时,它会在每一行中设置我的 UIlabel 的增量值。如何摆脱这个?

附言在 cellForRowAtIndex 路径中,我将标签的值设置为

lblCounterDisplay.text = [NSString stringwithformat:@"%d",counter];

而计数器是我的全局值,默认设置为 0。

编辑: 这是我的全部代码

 // global variables
int dynamicHeight;
bool flag=NO;
int indexPathCounter = 0;
int displayValue  = 0;

  - (void)viewDidLoad
  {
     [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


table.delegate = self;
table.dataSource = self;



arrForSec1 = [[NSMutableArray alloc] initWithObjects:@"test",@"tes",@"test",@"test",@"tes",@"test", nil];
}


// table view delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [arrForSec1 count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier];
    }



    UIButton *btnplus = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 23, 23)];
    [btnplus setTitle:@"+" forState:UIControlStateNormal];
    [cell addSubview:btnplus];
    [btnplus addTarget:self action:@selector(testCounter:) forControlEvents:UIControlEventTouchUpInside];
    btnplus.tag = indexPathCounter;
    btnplus.backgroundColor = [UIColor blackColor];



    UILabel *lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(160, 10, 23, 23)];
    lblCounter.text = [NSString stringWithFormat:@"%d",displayValue];
    lblCounter.backgroundColor = [UIColor orangeColor];
    lblCounter.textAlignment =UITextAlignmentCenter;
    [cell addSubview:lblCounter];


    UIButton *btnMinus = [[UIButton alloc] initWithFrame:CGRectMake(285, 10, 23, 23)];
    [btnMinus setTitle:@"+" forState:UIControlStateNormal];
    [cell addSubview:btnMinus];
    btnMinus.tag = indexPathCounter+1;
    [btnMinus addTarget:self action:@selector(testCounter:) forControlEvents:UIControlEventTouchUpInside];
    btnMinus.backgroundColor = [UIColor blackColor];

    indexPathCounter = indexPathCounter+2;


    return cell;
}


-(void)testCounter:(id)sender
{
    NSLog(@"kuch aya ? %ld",(long)[sender tag]);
    if([sender tag] % 2 == 0)
    {
       NSLog(@"plus!");
       displayValue = displayValue +1;

    }
    else
    {
        NSLog(@"its minus!");
        if(displayValue != 0)
    {
        displayValue = displayValue -1;

    }


}
    [table reloadData];
}

最佳答案

您的计数器变量应该是一个 NSArray of counter,索引与行一样多。

然后您会将每一行与其计数器匹配

 lblCounterDisplay.text = [NSString stringWithFormat:@"%@", [counter objectAtIndex:indexPath.row]];

编辑:

您的全局变量是问题的主要根源。

int indexPathCounter = 0;
int displayValue  = 0;

您需要使用 NSMutableArray 跟踪每一行计数。

进行以下更改:

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    table.delegate = self;
    table.dataSource = self;

    arrForSec1 = [[NSMutableArray alloc] 
                   initWithObjects:@"test",@"tes",@"test",@"test",@"tes",@"test", nil];
    //this is the array that will keep track of each count
    arrForCounters = [NSMutableArray arrayWithCapacity:[arrForSec1 count]];

    for (NSString *testString in arrForSec1) {
        [arrForCounters addObject:@"0"];
    }

}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }



    UIButton *btnplus = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 23, 23)];
    [btnplus setTitle:@"+" forState:UIControlStateNormal];
    [cell addSubview:btnplus];
    [btnplus addTarget:self action:@selector(increaseCounter:) forControlEvents:UIControlEventTouchUpInside];
    btnplus.tag = indexPath.row;
    btnplus.backgroundColor = [UIColor blackColor];



    UILabel *lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(160, 10, 23, 23)];
    lblCounter.text = [NSString stringWithFormat:@"%@", arrForCounters[indexPath.row]];
    lblCounter.backgroundColor = [UIColor orangeColor];
    lblCounter.textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter; (deprecated!)
    [cell addSubview:lblCounter];


    UIButton *btnMinus = [[UIButton alloc] initWithFrame:CGRectMake(285, 10, 23, 23)];
    [btnMinus setTitle:@"-" forState:UIControlStateNormal];
    [cell addSubview:btnMinus];
    btnMinus.tag = indexPath.row + 1000;
    [btnMinus addTarget:self action:@selector(decreaseCounter:) forControlEvents:UIControlEventTouchUpInside];
    btnMinus.backgroundColor = [UIColor blackColor];


    return cell;
}

为了清楚起见,我将您的 testCounter 函数拆分为 2 个方法:

-(void)increaseCounter:(id)sender
{
    int tag = [sender tag];
    int counter = [arrForCounters[tag]integerValue];

    arrForCounters[tag] = [NSString stringWithFormat:@"%d", counter + 1];

    [table reloadData];
}



-(void)decreaseCounter:(id)sender
{
    int tag = [sender tag];
    int counter = [arrForCounters[tag - 1000]integerValue];


    if (counter!=0) {
        arrForCounters[tag - 1000] = [NSString stringWithFormat:@"%d", counter - 1];

        [table reloadData];
    }

}

我创建了一个 SAMPLE PROJECT 希望对您有所帮助。

关于ios - 在 cellForRowAtIndexPath 中设置 UILabel 的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737338/

相关文章:

objective-c - 使用 Mail.app 从 Cocoa 发送 HTML 邮件

ios - NSPredicate 用于测试 NULL 和空白字符串

ios - 移动基板 : Undefined symbols for architecture armv7: "_main"

ios - 构建聊天应用程序时应该使用 UITableView 还是 UICollectionView?

objective-c - 查找 Mac OSX 序列号

ios - 编译时"No space left on device"错误

ios - 如何从推送 Controller 获取 RootViewController?

ios - Sqlite 结果集包含空值

ios - 如何在 Swift 中制作放置在我的 UIViewController 中的水平可滚动组件?

ios - UISearchController UITextField 背景在更新到 iOS 13 后不显示白色