在cellForRowAtIndexPath中设置UILabel的计数

时间:2014-03-29 21:49:01

标签: ios iphone objective-c

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

P.S。在cellForRowAtIndex路径中我将标签的值设置为

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

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

1 个答案:

答案 0 :(得分:1)

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

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

 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函数分为两种方法:

-(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 ,希望对您有帮助。