ios - 在 cellForRowAtIndexPath 之外访问自定义单元格属性

标签 ios objective-c uibutton

我想要像下图这样的东西。用户可以单击 + 或 - 按钮,增加或减少的计数将显示在 UILabel 中,即下图中的 1。
我知道如何实现自定义表格 View 单元格。我也知道如何实现选择器。但我需要根据单击的按钮设置标签文本,这只是问题所在。 我如何更新单击的按钮设置自定义单元格的 UILabel 文本在 cellForRowAtIndexPathwith 之外 添加减去功能。

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

    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

     static NSString *simpleTableIdentifier = @"ProductListVIICell";

     ProductListVIICell *cell = (ProductListVIICell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductListVIICell" owner:self options:nil];
         cell = [nib objectAtIndex:0];
     } ....................................
        cell.btnMinus.tag = indexPath.row;
        cell.btnPlus.tag = indexPath.row;

        [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];

        cell.lblCount.tag = [[NSString stringWithFormat:@"%d%d",addBtnClickCount,addBtnClickCount] integerValue];


        return cell;
    }

我需要在这里做一些事情,但没有得到我想要实现的目标。 cell.lblTitle.text 不工作

  #pragma mark - UIButton selector

-(void)addItem:(UIButton*)button {
    itemCount++;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
    ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];
      cell.lblCount.textColor = [UIColor redColor];
     cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
    NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
    [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
     NSLog(@"%d",itemCount);
    [self.tableView reloadData];


}

-(void)deleteItem:(UIButton*)button {
    itemCount--;
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
    ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];

    cell.lblCount.textColor = [UIColor redColor];
    cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
    NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
    [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
     NSLog(@"%d",itemCount);
    [self.tableView reloadData];

}

enter image description here

这些链接也很有用

Accessing cell attributes outside of cellForRowAtIndexPath

http://code.tutsplus.com/tutorials/blocks-and-table-view-cells-on-ios--mobile-22982 但它仅适用于 UITableViewCell 而不是自定义单元格

我能够使用以下方法解决我的问题

@interface ProductListVC ()
{
    int count;
    bool addBtnClicked;
    bool minusBtnClicked;


}

@property (strong,nonatomic) NSMutableArray* quantityArray; //stores label
@property (strong,nonatomic) NSMutableArray* quantityLabelArray; //stores label

- (void)viewDidLoad {

    [super viewDidLoad];

    _quantityLabelArray = [[NSMutableArray alloc] init];
     _quantityArray = [[NSMutableArray alloc] init];

//determine num of row prevent index 2 beyond bounds for empty array error i.e insert straight to let's say index 3
    for (int i=0; i <=[_productArray count]; i++) {
            [_quantityArray addObject:@"0"];

        }
}


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

if (!_quantityArray || !_quantityArray.count){
        cell.lblCount.text = [NSString stringWithFormat:@"%d", 0];

    }else{
        cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
     }
     [_quantityLabelArray addObject:cell.lblCount];


    if (addBtnClicked || minusBtnClicked) {
        //If add or minu button is reloading the cell

        cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
        //NSLog(@"%@",cell.lblCount.text);
        [_quantityLabelArray replaceObjectAtIndex:indexPath.row withObject:cell.lblCount];
        addBtnClicked = NO;
        minusBtnClicked = NO;

    }
    else
    {

    }

    cell.btnMinus.tag = indexPath.row;
    cell.btnPlus.tag = indexPath.row;
    cell.lblCount.tag = indexPath.row;

    [cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



#pragma mark - UIButton selector

-(void)addItem:(UIButton*)button

{

     NSLog(@"btn tag %ld",(long)[button tag]);
     NSLog(@"quantitylblarry %@",_quantityLabelArray);

        UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];
        if ([lblShowCount.text integerValue]< 10) {
            count = count+1;
            lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]+1];
        }

     NSLog(@"quantity array count %lu",(unsigned long)_quantityArray.count);

        if (_quantityArray.count > [button tag] ) {

            [_quantityArray removeObjectAtIndex:[button tag]];
            [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
        }

        else{

            [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
        }
        addBtnClicked = YES;

        NSLog(@"quantity array %@",_quantityArray);
        NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
        NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
        [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

    }





-(void)deleteItem:(UIButton*)button{

     NSLog(@"%ld",(long)[button tag]);
    UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];


        if ([lblShowCount.text integerValue]>=1) {
            count = count-1;
            lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]-1];

        }

        NSLog(@"%lu",(unsigned long)_quantityArray.count);

        if (_quantityArray.count > [button tag] ) {
            [_quantityArray removeObjectAtIndex:[button tag]];
            [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
        }
        else{
            [_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];

        }
        NSLog(@"%@",_quantityArray);
        minusBtnClicked =YES;

        NSLog(@"quantity array %@",_quantityArray);
        NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
        NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
        [self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

 }

最佳答案

最好使用自定义单元格。检查这个tutorials或者这个 one ,那么您需要将委托(delegate)添加到此单元格,以便您可以与 ViewController 同步数量

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.itemIndex = indexPath.row;
    ....
    return cell
}

更新

如果您有一个自定义单元格,您可以添加一个 UILabel socket 并在 +/- 按钮的操作中修改其文本,所有这些代码都将在 CustomTableViewCell.m

-(void)addItem:(UIButton*)button {    
    self.itemQuantity++; 
    [self updateQuantity];
}

-(void)deleteItem:(UIButton*)button {
    self.itemQuantity--;
    [self updateQuantity];
}
- (void)updateQuantity{
    self.lblCount.text = [NSStirng stringWithFormat:@"%d", self.itemQuantity];
    [self.delegate updateItemAtIndex:self.itemIndex withQuantity:self.itemQuantity];
}

更新:完整的解决方案

<强>1。型号

@interface Item : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic) NSInteger quantity;
@end

@implementation Item

@end

<强>2。自定义单元格

@interface CustomItemTableViewCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *lblTitle;
@property (nonatomic, weak) IBOutlet UILabel *lblCount;
@property (nonatomic, assign) Item *item;
@end

@implementation CustomItemTableViewCell

- (void)updateCellWithItem:(Item *)item {
    self.item = item;
    [self updateCellView];
}
- (void)updateCellView {
    self.lblTitle.text = self.item.title;
    self.lblTitle.text = [NSString stringWithFormat:@"%ld", self.item.quantity];
}
- (IBAction)addItem:(UIButton*)button {
    self.item.quantity++;
    [self updateCellView];
}

- (IBAction)deleteItem:(UIButton*)button {
    self.item.quantity--;
    [self updateCellView];
}

@end

<强>3。 TableView Controller

@interface ItemsTableViewController : UITableViewController
@property (nonatomic, strong) NSArray *items;
@end

@implementation ItemsTableViewController

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell" forIndexPath:indexPath];

    [cell updateCellWithItem:self.items[indexPath.row]];

    return cell;
}

关于ios - 在 cellForRowAtIndexPath 之外访问自定义单元格属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31049551/

相关文章:

ios - 如何使用其他系统图标?

ios - 带有空数组的 UITableView reloadData

iphone - 我无法通过按回车键隐藏键盘

ios - 使用NSArray时,避免“越界”的最佳方法是什么?

ios - 如何使用ios, objective-c 获取与json中的对象具有相同引用号的所有值

objective-c - 如何从 NSString 中提取/拆分数字和字符串

ios - 长按后如何跟踪按钮选择?

iOS 8.1 模拟器总是使用美国键盘布局,尽管德国硬件键盘

ios - 将父类作为目标添加到 UIButton

iphone - 如何在 iOS 上创建漂亮的按钮?