Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 访问CellForRowatineXpath之外的自定义单元格属性_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 访问CellForRowatineXpath之外的自定义单元格属性

Ios 访问CellForRowatineXpath之外的自定义单元格属性,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我想要一些像下图这样的东西。用户可以单击+或-按钮,增加或减少的计数显示在UILabel中,即下图中的1。 我知道如何实现自定义表视图单元格。我也知道如何实现选择器。但是我需要根据点击的按钮设置标签文本,这才是问题所在。 如何更新单击的按钮和设置自定义单元格的UILabel文本,从cellForRowAtIndexPathwith添加-减去功能外部 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexP

我想要一些像下图这样的东西。用户可以单击+或-按钮,增加或减少的计数显示在UILabel中,即下图中的1。

我知道如何实现自定义表视图单元格。我也知道如何实现选择器。但是我需要根据点击的按钮设置标签文本,这才是问题所在。 如何更新单击的按钮设置自定义单元格的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;
    }
我需要在这里做事,但没有得到我想要的<代码>单元格.lbltite.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];

}

这些链接也很有用

但它仅适用于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];

 }
@interface ProductListVC()
{
整数计数;
bool addbtcled;
bool minusbtcled;
}
@属性(强,非原子)NSMutableArray*quantityArray//商店标签
@属性(强,非原子)NSMutableArray*quantityLabelArray//商店标签
-(无效)viewDidLoad{
[超级视图下载];
_quantityLabelArray=[[NSMutableArray alloc]init];
_quantityArray=[[NSMutableArray alloc]init];
//确定行数防止索引2超出空数组错误的界限,即直接插入到索引3
对于(int i=0;i[按钮标记]){
[\u quantityArray removeObjectAtIndex:[按钮标记]];
[\u quantityArray insertObject:lblShowCount.text atIndex:[按钮标记]];
}
否则{
[\u quantityArray insertObject:lblShowCount.text atIndex:[按钮标记]];
}
addBtnClicked=是;
NSLog(@“数量数组%@”,_quantityArray);
nsindepath*rowToReload=[nsindepath indexPathForRow:[button tag]instation:0];
NSArray*rowsToReload=[NSArray数组及其对象:rowsToReload,nil];
[self.tableView重新加载rowsatindexpaths:rowstorealoadwithrownanimation:uitableviewrownanimationnone];
}
-(无效)删除项:(UIButton*)按钮{
NSLog(@“%ld”(长)[按钮标记]);
UILabel*lblShowCount=[\u quantityLabelArray对象索引:[按钮标记]];
如果([lblShowCount.text integerValue]>=1){
计数=计数-1;
lblShowCount.text=[NSString stringWithFormat:@“%ld”,[lblShowCount.text integerValue]-1];
}
NSLog(@“%lu”,(无符号长)\u quantityArray.count);
如果(_quantityArray.count>[按钮标记]){
[\u quantityArray removeObjectAtIndex:[按钮标记]];
[\u quantityArray insertObject:lblShowCount.text atIndex:[按钮标记]];
}
否则{
[\u quantityArray insertObject:lblShowCount.text atIndex:[按钮标记]];
}
NSLog(@“%@”,_quantityArray);
minusBtnClicked=是;
NSLog(@“数量数组%@”,_quantityArray);
nsindepath*rowToReload=[nsindepath indexPathForRow:[button tag]instation:0];
NSArray*rowsToReload=[NSArray数组及其对象:rowsToReload,nil];
[self.tableView重新加载rowsatindexpaths:rowstorealoadwithrownanimation:uitableviewrownanimationnone];
}

最好使用自定义单元格。选中此项或此项,则需要将委托添加到此单元格,以便可以与ViewController同步数量

i、 e

更新


如果您有一个自定义单元格,您可以添加一个
UILabel
出口,并通过
+/-
按钮修改其文本,所有这些代码都将位于
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
@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
2。自定义单元格

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

@implementation Item

@end
@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。TableViewController

@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;
}

要更改单元格标签文本,您需要在这些选择器中提取单元格对象,您只需使用sender参数(在您的示例中为按钮)即可。有关添加功能,请参见下面的代码

-(void)addItem:(UIButton*) button
{    
  NSLog(@"UIButton%ld",(long)button.tag);
  addBtnClickCount++;  //Increase the count by 1
  MyCustomCell *cell = button.superview.superview;
  //Now change the label text for cell
  cell.lblCount.text = [NSString stringWithFormat:@"%d",addBtnClickCount];
}

使用减法功能的方法相同。

在自定义tableview单元格中使用更陡的控件

-(void)addItem:(UIButton*) button
{    
  NSLog(@"UIButton%ld",(long)button.tag);
  addBtnClickCount++;  //Increase the count by 1
  MyCustomCell *cell = button.superview.superview;
  //Now change the label text for cell
  cell.lblCount.text = [NSString stringWithFormat:@"%d",addBtnClickCount];
}

customcell.h类

@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property (weak, nonatomic) IBOutlet UILabel *lbl_value;
****customcell.m****

- (IBAction)stepper:(UIStepper *)sender {
    NSUInteger value = sender.value;
    lbl_value.text = [NSString stringWithFormat:@"%03d",value];
    self.txt1.text= [NSString stringWithFormat:@"%03d",value];

}

我知道如何实现自定义表视图单元格。你能再核对一下这个问题吗。我也知道如何实现选择器。但是我需要根据单击的按钮来增加和减少标签文本,这只是问题所在。如果您有一个自定义单元格,您将添加一个
UILabel
出口,并通过
+/-
按钮来修改其文本,所有这些代码都将在
CustomTableViewCell.m
中,所以您将代码移动到CustomCell类中了?你确定你添加了出口并在.h文件中定义了代理吗?目前我已经为你的努力向你投票,谢谢你,我仍然有gt的麻烦你能检查编辑的问题吗?你真的认为可以从CellForRowAtIndexPath外部执行cell.lblCount.text我自己还没有尝试过。但是应该是这样的,因为您只是在修改单元格对象。你可以检查一下我修改过的问题吗?你为什么要在
addItem:
方法中计算第1节的indexath。我在图像或
cellForRow…
的代码中没有看到任何部分。也许这就是原因。我现在已经测试了我的逻辑,它是有效的,所以可能还有其他原因。尝试使用调试器。打印表视图中的所有单元格对象,然后打印在
addItem:
方法中得到的单元格,看看它们是否是同一个对象;您应该看到这一点。我在swift中完成了,但在目标c中没有。我知道UITableViewCell中UIButton的操作,但问题是每次用户单击buttoncell.lblTitle.text=“value changed”按钮时都要设置UILabel;这行得通吗?你所做的一切似乎都是正确的不,它行不通