Accessing custom cell attributes outside of cellForRowAtIndexPath

前端 未结 3 697
悲&欢浪女
悲&欢浪女 2020-12-11 22:07

I want some thing like image below. User can click on + or - button and the increased or decreased count is display in UILabel i.e 1 in the image below.
I know how to im

3条回答
  •  不思量自难忘°
    2020-12-11 23:02

    it is better to use custom cell. Check this tutorials or this one, then you will need to add delegate to this cell so that you can sync quantities with you ViewController

    i.e.

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

    UPDATE

    if you have a custom cell, you would add an UILabel outlet and modify its text in the action of +/- buttons, all this code will be in 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];
    }
    

    UPDATE: COMPLETE SOLUTION

    1. Model

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

    2. Custom Cell

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

提交回复
热议问题