2013-08-07 3 views
0

У меня есть две кнопки & ярлык в UITableViewCell с именем +, - & countLabel.Как решить проблему прокрутки в виде таблицы

Начальный счетчик должен быть 0. Когда я нажимаю кнопку +, то countLabel увеличивается на 1 для щелкнутой ячейки, а когда я нажимаю кнопку - тогда суммарный счетчик уменьшается на -1 для нажатой ячейки. Он работает отлично.

Но моя проблема заключается в том, когда я просматриваю вид таблицы .. текст countLabel отображается во всех ячейках таблицы.

Я пробовал это?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 

static NSString *CellIdentifier = @"productListCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
NSString *title; 
UILabel *productNameLbl; 
UILabel *countLbl; 
NSMutableDictionary *productListCellDit; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    productNameLbl =[[UILabel alloc]init]; 
    [productNameLbl setFrame:CGRectMake(53, 2, 120, 59)]; 
    [productNameLbl setTextColor:[UIColor blackColor]]; 
    [productNameLbl setBackgroundColor:[UIColor clearColor]]; 
    [productNameLbl setTextAlignment:NSTextAlignmentLeft]; 
    [productNameLbl setFont:[UIFont boldSystemFontOfSize:15.0]]; 
    productNameLbl.lineBreakMode=NSLineBreakByWordWrapping; 
    productNameLbl.numberOfLines=3; 
    productNameLbl.tag=2; 
    [cell.contentView addSubview:productNameLbl]; 

    countLbl =[[UILabel alloc]init]; 
    [countLbl setFrame:CGRectMake(56, 60, 117, 12)]; 
    [countLbl setTextColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0]]; 
    [countLbl setBackgroundColor:[UIColor clearColor]]; 
    [countLbl setTextAlignment:NSTextAlignmentLeft]; 
    [countLbl setFont:[UIFont boldSystemFontOfSize:15.0]]; 
    countLbl.tag=3; 
    [cell.contentView addSubview:countLbl]; 


    decreaseBtn =[UIButton buttonWithType:UIButtonTypeCustom]; 
    [decreaseBtn setTitle:@"-" forState:UIControlStateNormal]; 
    [decreaseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal]; 
    [decreaseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted]; 
    [[decreaseBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]]; 
    [decreaseBtn setFrame:CGRectMake(259, 2, 30, 71)]; 
    decreaseBtn.tintColor=[UIColor lightGrayColor]; 
    decreaseBtn.tag=indexPath.row; 
    [decreaseBtn setBackgroundImage:[UIImage imageNamed:@"-+BtnImg.png"] forState:UIControlStateNormal]; 
    [decreaseBtn addTarget:self action:@selector(decreaseItemCount:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:decreaseBtn]; 

    increaseBtn =[UIButton buttonWithType:UIButtonTypeCustom]; 
    [increaseBtn setTitle:@"+" forState:UIControlStateNormal]; 
    [increaseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal]; 
    [increaseBtn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted]; 
    [[increaseBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]]; 
    [increaseBtn setFrame:CGRectMake(289, 2, 30, 71)]; 
    increaseBtn.tintColor=[UIColor lightGrayColor]; 
    increaseBtn.tag=indexPath.row; 
    [increaseBtn setBackgroundImage:[UIImage imageNamed:@"-+BtnImg.png"] forState:UIControlStateNormal]; 
    [increaseBtn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:increaseBtn]; 
} else { 
    productNameLbl =(UILabel *)[cell.contentView viewWithTag:2]; 
    countLbl =(UILabel *)[cell.contentView viewWithTag:3]; 
    decreaseBtn =(UIButton *)[cell.contentView viewWithTag:indexPath.row]; 
    increaseBtn =(UIButton *)[cell.contentView viewWithTag:indexPath.row]; 
} 
productListCellDit=[productListArry objectAtIndex:indexPath.row]; 
title= [productListCellDit objectForKey:@"name"]; 

productNameLbl.text=title; 
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount]; 

return cell; 

} 

//to increment count 
-(void)increaseItemCount:(UIButton *)sender 

{ 
    UITableViewCell *cell = (UITableViewCell *)sender.superview.superview; 
NSIndexPath *path = [listOfProductsTable indexPathForCell:cell]; 
NSLog(@"row: %d",path.row); 
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:3]; 
showItemCount=[countLbl.text intValue] + 1; 
NSLog(@"%d",showItemCount); 
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount]; 

    } 

    //to decrement count 
    -(void)decreaseItemCount:(UIButton *)sender 
    { 
    if (!showItemCount==0) { 
    UITableViewCell *cell = (UITableViewCell *)sender.superview.superview; 
    NSIndexPath *indexPath = [listOfProductsTable indexPathForCell:cell]; 
    NSLog(@"row: %d",indexPath.row); 
    UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:3]; 
    showItemCount=[countLbl.text intValue] - 1; 
    NSLog(@"%d",showItemCount); 
    countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount]; 
    } 
    } 

В ViewDidLoad

showItemCount=0 
+0

показать, что вы попробовали ??? –

+0

код пожалуйста ...... –

+0

please post ur код – Xcode

ответ

1

Вы должны установить тег лейбла = 3 в cellForRowAtIndexPath способом, а затем сделать это.

-(void) increaseItemCount:(UIButton *) sender 
{ 
    // ----- edited ---- 
    NSIndexPath *path = [NSIndexPath indexPathForRow:[sender.tag intValue] inSection:section]; 

    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:path] contentView] viewWithTag:3]; 
    content.text = [content.text intValue]+1; 
} 

-(void) decreaseItemCount:(UIButton *) sender 
{ 
    NSIndexPath *path = [NSIndexPath indexPathForRow:[sender.tag intValue] inSection:section]; 

    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:path] contentView] viewWithTag:3]; 
    content.text = [content.text intValue]-1; 
} 
+0

Пожалуйста, проверьте мой отредактированный вопрос. В том, что я получаю счет, но он не работает для определенной выбранной ячейки. Он работает для всей камеры. –

+0

Когда вы нажимаете кнопку в то время, вам нужно передать определенный путь индекса. –

+0

может у вас предложить мне, как это сделать –