Retrieving data from a custom UITableViewCell and back to a UITableViewController

I have UITableView, consisting of custom UITableViewCells. Each cell has UILabela UISlider. Does anyone know how, when changing the value of one of the sliders, send the new value of the slider from the user UITableViewCell(in a separate file) to UITableViewControllerso that I can then update the array from which the table was filled?

The closest I still have is an unsuccessful hack: triggering an event setSelectedwhen the value of the slider changes. As long as this emphasizes the changed custom cell, the event is not matched didSelectRowAtIndexPathto UITableViewController.

Custom UITableViewCells Although code is always welcome, a conceptual / methodical solution is what I am looking for.

Thanks in advance, Jamie

+4
1

, , .

, , :

- , . - - . , . , , , . .


, :

:

enter image description here

:

enter image description here

, , , .


Objective-C:

, - UITableViewCell. ContactTableViewCell.

ContactTableViewCell.h :

@protocol ContactCellDelegate <NSObject>
@required


-(void) didMoveSliderWithValue:(float) value;

@end



@interface ContactTableViewCell : UITableViewCell


@property (weak, nonatomic) id<ContactCellDelegate> delegate;

TableViewController . VC MyTableViewController.

MyTableViewController.h :

@interface MyTableViewController : UIViewController <ContactCellDelegate> //Use UITableViewController if you are using that instead of UIViewController.

cellForRowAtIndexPath :

cell.delegate = self;

MyTableViewController.m.

-(void) didMoveSliderWithValue: (float) value
{
    NSLog(@"Value is : %f",value);
    //Do whatever you need to do with the value after receiving it in your VC
}

ContactTableViewCell.m. IBAction . , :

- (IBAction)sliderValueChanged:(UISlider *)sender {

    self.myTextLabel.text = [@((int)sender.value) stringValue]; //Do whatever you need to do in cell.

 //Now call delegate method which will send value to your view controller:

   [delegate didMoveSliderWithValue:sender.value];

}

, , MyTableViewController. , .

, , VC ( Cell), ", , . ", VC , , .


Swift:

, TableViewCell.swift , :

@class_protocol protocol ContactCellDelegate {
    func didMoveSliderWithValue(value: Float)
}

Cell , :

var cellDelegate: ContactCellDelegate?

IBAction :

self.cellDelegate?.didMoveSliderWithValue(slider.value)

VC :

:

class MyTableViewController: UIViewController, ContactCellDelegate

cellForRowAtIndexPath

cell.cellDelegate = self //Dont forget to make it conform to the delegate method

:

func didMoveSliderWithValue(value:float) {
            //do what you want
        }

Swift , Obj-C Swift. , .

.: fooobar.com/questions/5343/...

+6

Source: https://habr.com/ru/post/1627739/


All Articles