I have a TableView and I am Using Custom TableCell.In TableCell I have a button. Now in TableView I have created a Bool Value to check whether the Check button is Selected or not.
The problem is When I scroll the Table the cell get reused so the button gets Automatically deselected.
if (checkButton.selected==NO) {
NSLog(#"ooo");
checkButton.selected=YES;
checkSelected=YES;
}
else {
NSLog(#"Okkkkk");
checkButton.selected=NO;
checkSelected=NO;
}
I have an idea to Subclass UIButton with a custom Property of BOOl.But I dont know to workout. Can anyone help me out?
You can keep a track of button selected using the array. Just set value 1 at particular index when button is selected. And set it to 0 when it's deselected.
And then while creating the cell you can check in as follows:
if ([[arr_Check objectAtIndex:indexPath.row] isEqualToString:#"0"])
checkButton.selected=NO;
else
checkButton.selected=YES;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%i%i",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else
{
NSArray *cellSubs = cell.contentView.subviews;
for (int i = 0 ; i < [cellSubs count] ; i++)
{
[[cellSubs objectAtIndex:i] removeFromSuperview];
}
}
UICheckBox *checkBox = [[UICheckBox alloc] initWithFrame:CGRectMake(0, 0, 44.0, 44.0)];
checkBox.tag = indexPath.row;
checkBox.titleLabel.tag = indexPath.section;
[checkBox addTarget:self action:#selector(checkBoxPressed:) forControlEvents:UIControlEventTouchUpInside];
for (NSIndexPath *temp in selectedObjects) {
if ([temp isEqual: indexPath]) {
checkBox.selected = TRUE;
}
}
[cell.contentView addSubview:checkBox];
return cell;
}
-(void)checkBoxPressed:(UICheckBox*)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:sender.titleLabel.tag];
if (sender.selected)
[selectedObjects removeObject:indexPath];
else
[selectedObjects addObject:indexPath];
sender.selected = !sender.selected;
NSLog(#"\n\n%#",selectedObjects);
}
Here selectedObjects is a global NSArray and UICheckBox is a subClass of UIButton
#interface CustomButton : UIButton
#property(nonatomic) BOOL isSelected;
// DO NOT FORGET TO SYNTHESIZE MY isSelected
#end
since you are using a custom cell. you can just make a property CustomButton in your custom cell.
#interface YourCustomCell : UITableViewCell
#property(nonatomic, strong) CustomButton *button;
#end
in your cellForRowAtIndexPath method
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellType];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellType owner:nil options:nil];
cell = (YourCustomCell*)[nib objectAtIndex:0];
}
//you can add a target to your button here
cell.button addTarget:self action:#selector(changeButtonState:).....
if(cell.button.isSelected == YES)
NSLog(#"selected");
else
NSLog(#"not selected");
-(void)changeButtonState:(CustomButton*)button
{
if(button.isSelected) button.isSelected = NO;
else button.isSelected = YES;
[yourTableView reloadData];
}
Related
I am new to iphone.I have a small question that is,I have a class CustomCell in that i have declare all elements which i have required like the below
// CustomCell.h
here in this class i am setting the getter and setter methods to TitleLabel, PercentageLabel, ProgressView, downloadButton
// CustomCell.m
here in this class set the frame to above all,and set the image to the download button also here
screen shot of my output screen is
// In my table view i have a download button when we click that the following method will executes
-(void)downloadButtonClicked:(id)sender
{
NSLog(#"download button clicked");
int index = [sender tag];
NSLog(#"index of the cell is %d",index);
UIButton *button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
NSLog(#"label text =%#",titleLabel.text);
UIView *senderButton = (UIView*) sender;
NSIndexPath *indexPath = [tableView indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
NSLog(#"indexpath is %d",indexPath.row);
CustomCell *customCell = [[CustomCell alloc]init];
progressView = [[UIProgressView alloc]init];
// [customCell.contentView insertSubview:progressView atIndex:indexPath.row];
[customCell.contentView addSubview:progressView];
selectedBookTitle = titleLabel.text;
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *allDownloadLinks;
biblePlayerViewController = [[BiblePlayerViewController alloc]init];
allDownloadLinks = [biblePlayerViewController allDownloadLinks];
NSLog(#"all Download Links are %#",allDownloadLinks);
biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];
Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.audiotreasure.com/%#.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
[[DownloadManager sharedDownloadManager] queueDownload: download];
}
// In cellForRowAtIndexPath tableview delegate method I have written code like this
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSLog(#"indexpath.row is %d",indexPath.row);
NSLog(#"downloadmanager is %d",[[[DownloadManager sharedDownloadManager]getDownloads]count]);
/* if (indexPath.row<[[[DownloadManager sharedDownloadManager] getDownloads]count])
{
cell.TitleLabel.text = ((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).title_;
cell.ProgressView.progress = (float)(((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_ )/100.0f;
cell.PercentageLabel.text = [NSString stringWithFormat:#"%d %%",((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_];
}
else
{
[tableView reloadData];
}*/
NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
cell.TitleLabel.text = titleLabel;
cell.downloadButton.tag = indexPath.row;
NSLog(#"tag is %d",cell.downloadButton.tag);
[cell.downloadButton addTarget:self action:#selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
My question is I have 66 cells in tableview in screen shot we have a bookname called Revelation.It has a 66th cell in my tableview (index is 65).Here when i click the download button in that cell,I have to show progress view in that particular cell only (65th cell).I have reload the table view for every 1 sec also because after placing the progress view we have to show the progress in that progress view in that cell.So, how can we place a progress view in that particular cell index.
Why don't you add the progress view in the cell from the beginning?
have it initially hidden and when you touch the download button you tell the cell to show it.
i see that you store the indexPath.row of the cell in the download button tag. You could use that to find out what cell was selected something like below.
NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:nowIndex];
[cell showProgressBar];
i wrote the code of top of my head..didn't tested it.
How to make UITableView like above image?
I know this is grouped table type. but how we can add the image+label+button to header of section.
I have tried
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
but it starts with CGRectMake(0,0,320,height).
I want just above section and exact width of section just like in image.
Thanks in advance.
Instead of trying to change the section header view, you might want to create a custom cell with a brown background, a label and a button and use it for the first row. So, in -cellForRowAtIndexPath, you could do something like
if (0 == indexPath.row) {
return brownCell;
} else {
return normalCell;
}
There are several ways to create custom cells, I always start from the Table View Programming Guide for iOS.
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell{
id delegate;
NSIndexPath *indexpath;
}
#property(nonatomic,assign) id delegate;
#property(nonatomic,retain)NSIndexPath *indexpath;
#property(nonatomic,retain) IBOutlet UIToolbar *Toolbar;
-(IBAction)SelectorLeft:(id)sender;
-(IBAction)SelectorRight:(id)sender;
#end
customcell.m
#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
#implementation CustomCell
#synthesize Toolbar;
#synthesize delegate,indexpath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)SelectorLeft:(id)sender{
[delegate perfromselector:#selector(left:) withObject:indexpath];
}
-(IBAction)SelectorRight:(id)sender{
[delegate perfromselector:#selector(left:) withObject:indexpath];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
UItbaleView part
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"identifier";
NSUInteger row = [indexPath row];
if (row == 0) {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
self.Cell = nil;
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.Cell;
}
cell.Toolbar.clipsToBounds=YES;
CALayer *l=cell.Toolbar.layer;
// set corner radious
[l setCornerRadius:10];
// to apply border on corners
[l setBorderColor:[[UIColor clearColor] CGColor]];
// to apply set border width.
[l setBorderWidth:5.0];
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat: #"cell %i",row];
cell.delegate = self;
cell.indexpath = indexpath;
return cell;
}
return nil;
}
Also do n't forget to create Customcell.xib and add tool bar through interface builder
also create an outlet of CustomCell in tableview class and handle it as above
its easy all what you have to
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
if (row % 2 == 0) {
static NSString *identifier = #"RowOne";
UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
}
cell.Title.text = [datasource objectatindex:row];
cell.Title.font = [UIFont fontWithName:#"Tahoma" size:16];
cell.contentView.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
return cell;
}else if (row % 2 == 1) {
static NSString *identifier = #"RowTwo";
UITableViewCell *cell = (RowTypeOne*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault]autorelease];
}
cell.contentView.backgroundColor = [UIColor redColor];
cell.Title.text = [datasource objectatindex:row];
cell.Title.font = [UIFont fontWithName:#"Tahoma" size:16];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textAlignment = UITextAlignmentRight;
return cell;
}
return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//create btn left each on handeled by selector
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithTitle:#"List of sms" style:UIBarButtonItemStylePlain target:self action:#selector(ListClicked:)];
//create btn right each on handeled by selector
UIBarButtonItem *btnright = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStyleBordered target:self action:#selector(NewClicked:)];
//create uitoolbar then add btns to it as list of array
UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//change style of toolbar to be black
[tool setBarStyle:UIBarStyleBlack];
tool.items = [NSArray arrayWithObjects:btnleft,[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],btnright, nil];
//this is the parent view that we will add on it the uitoolbar objects and the buttons
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[view autorelease];
[view addSubview:tool];
return view;
}
-(void)ListClicked:(id)sender{
//handle here btn left
}
-(void)NewClicked:(id)sender{
//handle here btn right
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
if you want the cell not to be in the header you can check for first row in cellforrow at index path...
also if you want to do it in another style you can make custom cell and add it also to cellforrowatindexpath
I have a UITableView with a special row at the end to insert a new item. That works, but I want it to have the green plus icon without having to put the table view into editing mode. How can I do that?
I'd prefer not to create a button or bundle the image, if possible. Is there any way to do either or both of those things using only standard UITableView/UITableViewCell features?
you want to set the accessoryView to the cell:
#interface RootViewController : UITableViewController {
NSInteger nextValue;
NSMutableArray *timeIntervals;
}
#implementation RootViewController
- (NSNumber *)nextValue {
NSNumber *n = [NSNumber numberWithInteger:nextValue];
nextValue++;
return n;
}
- (void)viewDidLoad {
[super viewDidLoad];
nextValue = 1;
timeIntervals = [[NSMutableArray alloc] init];
[timeIntervals addObject:[self nextValue]];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [timeIntervals count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TimeIntervalCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd];
[b addTarget:self action:#selector(addTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = b;
}
NSNumber *number = [timeIntervals objectAtIndex:indexPath.row];
cell.accessoryView.tag = indexPath.row;
cell.textLabel.text = [number stringValue];
cell.detailTextLabel.text = #"detail about this number";
return cell;
}
- (void)addTapped:(UIButton *)sender {
id cell = sender;
while (cell != nil && [cell isKindOfClass:[UITableViewCell class]] == NO)
cell = [cell superview];
if (cell == nil) {
NSLog(#"[%# %#] sender was not in a cell",
NSStringFromClass([self class]), NSStringFromSelector(_cmd));
return;
}
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSInteger index = indexPath.row + 1; // insert after current cell
[timeIntervals insertObject:[self nextValue] atIndex:index];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"[%# %#] not implemented", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
#end
(this is all the modified code to the Xcode 4.0.2 navigation app template)
You could implement your last cell as Custom Cell and add the green icon as per your choice.
See the tutorial to implement custom cell.
iPhone Programming Tutorial: Part 6: Creating custom UITableViewCell Using Interface Builder UITableView
Updated:
Let's say cell is the instance of UITabelViewCell.
First create a button using your green icon.
UIButton myGreenIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton addTarget:self action:#selector(GreenIconButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton setBackgroundImage:[UIImage imageNamed:#"greenIcon.png"] forState:UIControlStateNormal];
myGreenIconButton.tag = i;
myGreenIconButton.backgroundColor = [UIColor clearColor];
myGreenIconButton.frame = CGRectMake(5, 78, 15, 15);
Now add it as subview in your last UITabelViewCell.
[cell addSubview:myGreenIconButton];
Implement GreenIconButtonClicked: method to receive the click evrnt on you add green icon button
-(void) GreenIconButtonClicked:(id) sender
{
}
Unfortunately, the only way I've found to do this is to set the cell's image, which means you have to handle the image files yourself instead of having UIKit load them for you. I recommend using the UIKit Artwork Extractor to get the image.
I am working on an application having UItableView. In the rows of the table i am able to put checkmarks.Now how to save the state of checkmarks so that even if user close the application
the state should be shaved and in the next launch of application checkmark should be shown.
i have followed the tutorials on NSUSerDefaults but Pulling my hairs where to put the codes of saving and retrieving.I have tried but every time errors are stuffing me and not able to fix.
My Code:
MY.h file
**#protocol LocationSelectionViewControllerDelegate <NSObject>
#required
- (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index;
#end**
#interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{
UITableView *table;
***NSInteger selectedIndex;***
NSMutableArray *menuList;
***id <LocationSelectionViewControllerDelegate> delegate;***
}
#property (nonatomic,retain) NSMutableArray *menuList;
#property (nonatomic,retain) IBOutlet UITableView *table;
***#property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;**
**#property NSInteger selectedIndex;***
#end
my .m file:
#implementation LocationSelection
***#synthesize menuList, table,selectedIndex,delegate;***
- (void)viewDidLoad
{
menuList = [[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"LOCATION1", nil],
[NSArray arrayWithObjects:#"LOCATION2", nil],
[NSArray arrayWithObjects:#"LOCATION3", nil],
nil];
self.title = #"Location Selection";
[table reloadData];
[super viewDidLoad];
}
//MY CELLFORROWATINDEXPATH
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted = NO;
***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];***
UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease];
nameLabel.text = [NSString stringWithFormat:#"%#", [rowArray objectAtIndex:0]];
[cell.contentView addSubview:nameLabel];
***cell.accessoryType = (rowArray == selectedIndex && selectedIndex > -1 && selectedIndex < [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
***
}
//MY DIDSELECTROWATINDEXH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
if (newRow != selectedIndex)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
if (selectedIndex > - 1 && selectedIndex < [menuList count])
{
NSUInteger newIndex[] = {0, selectedIndex};
NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
selectedIndex = newRow;
NSString *selectedValue=[menuList objectAtIndex:selectedIndex];
[self.delegate rowSelected:selectedValue selectedIndex:selectedIndex];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
Use this method to save your data to NSUserDefaults
And set the data in viewDidLoad of LocationSelection
Hi I think it will suit you if you have an object(s) to indicate the checkmark. You can save this to the user defaults through synchronize. In the cellForRowATIndexPath: you can check if the value is present in the user defaults and if yes make the cell accessory as checkmarked and if its not present make it none.
Put a state in your menuList which reflects selection of the cell so even multiselection can be done easily. You can use a dictionary instead of the arrays. Arrays are fine but not as readable, so you have to remember which field contains what and map it to an index.
When the view loads load the menuList array from your userDefaults, when the app closes save the defaults.
In didSelectRowAtIndexPath you save selection in menuList.
In cellForRowAtIndexPath you read menuList and the new array index/dictionary key and set the checkmark.
I have TableCellViewController for managing cells in my UITableView. Each cell has a label na UISwitch (dictTypeSwitch). A want to assign method to switch events so I can save the state of button.
So far I've done this:
assign setState function to object:
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
function for handling event:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? #"YES" : #"NO";
NSLog(rez);
}
From sender I get UISwitch object from which I can get state.
So far everything is ok.
But if I want to save the state of UISwitch, I have to get rowIndex for this cell.
How can I achieve this?
The cells are made inside function cellForRowAtIndexPath. Se the code bellow:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"DictionariesTableCellViewController";
DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DictionariesTableCellViewController" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (DictionariesTableCellViewController *) currentObject;
break;
}
}
}
// Configure the cell...
cell.dictTypeLabel.text = [NSString stringWithFormat:#"row - %i",indexPath.row];
[cell.dictTypeSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Thanks
You can use the UISwitch's .tag property to store an arbitrary integer. This can be set to the row index.
If it is possible to find the UITableViewCell from UISwitch's parent, you could get the index path with
NSIndexPath* indexPath = [theTableView indexPathForCell:theCell];