Question

I'm a newbie in objective-c and iOS

I created several custom table cells with outlets in it. But I'm not sure if I use the right way.

What I want to do is to get the property of an Outlet which is inside the custom cell, by an IBAction defined in the table view controller.

I have a custom table cell called KolonNumberCell, and a UISlider in it. I want to define an IBAction called playNumbers in the UITableViewController. The slider seems to work fine in the custom cell. I can get its value.

But I cannot figure out how to get the value of this slider when the playButton outlet is tapped.

I would be so grateful if you could give me some information or show me a way.

Thanks in advance

TableController.h

#import <UIKit/UIKit.h>
@class KolonNumberCell;
@class BankoNumberCell;

@interface TableViewController : UITableViewController{
    NSArray *bundleArray;
    IBOutlet KolonNumberCell *kolonCell;
    IBOutlet BankoNumberCell *bankoCell;
    UIButton *playButton;
}

@property (strong) IBOutlet KolonNumberCell *kolonCell;
@property (strong) IBOutlet BankoNumberCell *bankoCell;
@property (nonatomic,retain) NSArray *bundleArray;
@property (nonatomic,retain) IBOutlet UIButton *playButton;

- (void)playNumbers:(id)sender;
@end

part of TableController.m

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize bundleArray,kolonCell,bankoCell,playButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *bundle1 = [[[NSBundle mainBundle] loadNibNamed:@"KolonNumberCell" owner:self options:nil] objectAtIndex:0];
    NSArray *bundle2 = [[[NSBundle mainBundle] loadNibNamed:@"BankoNumberCell" owner:self options:nil] objectAtIndex:0];

    bundleArray = [[NSArray alloc] initWithObjects:bundle1,bundle2, nil];

}

- (void)playNumbers:(id)sender
{

    NSLog(@"button");
    // Get the value of the slider in KolonNumberCell... but how?

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    tableView.allowsSelection = NO;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [bundleArray objectAtIndex:indexPath.section];
    }

    return cell;

}
@end

One of my customtableviewcell implementation file

#import "KolonNumberCell.h"

@implementation KolonNumberCell
@synthesize label,slider;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        // Initialization code

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

    self.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"doubleRound.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]];
}



- (IBAction)sliderChanged:(id)sender
{
    [label setText:[NSString stringWithFormat:@"%i",(int)slider.value]];
}

+ (NSString *)reuseIdentifier {
    return @"Cell";
}


@end
Was it helpful?

Solution

Your design is odd because your code will become really messy with more Cells. Matt Gallagher has a nice implementation for Custom TableViews. He is using the TableViewCell as a Controller and puts all the logic related to cell in the cells class. The discussion thread on Stack Overflow:

CustomTableViewController

If you still want to do it your way:

  1. Give your Slider a tag: slider.tag = x //Make sure that the tag is unique
  2. In PlayNumbers:

     UISlider *slider = (UISlider *)[[[bundleArray objectAtIndex:0] contentView] viewWithTag:x];
     NSLog(@"%i",(int)slider.value);
    

Edit: thanks for correcting the syntax

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top