I have a static tableview on a storyboard, however can you add a content like a image,button,switch to a certain cell from code .
I have created an IB outlet for the relevant cell, do I have to add a view inside 1st ?
To more specific I have a switch in a cell and want to add customise it with RCSwitch, I don't see how you can add a RCSwitch to the storyboard, as all examples show the switch being created programmatically.
my code is this
RCSwitchOnOff* onSwitch = [[RCSwitchOnOff alloc] initWithFrame:CGRectMake(0, 0, 65, 23)];
[onSwitch setOn:YES];
[self.oneRow.contentView addSubview:onSwitch];
// onerow is the IBOutlet for the row within the tableview
How did you even create an IBOutlet for a single cell? The only thing you normally should be able to to if you want to personalize your cell in a xib is create a custom cell and use it programmatically in your cellForRow: atIndexPath:. However, I think the simplest solution here would be to do everything in code, as your app seems to be simple:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = #"Cell";
UITableViewCell *yourCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!yourCell){
yourCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == yourDesiredCellIndexPath){
// Do your thing
}
return yourCell;
}
Related
I'm trying to create a custom UITableViewCell via Storyboard for my iPhone App (Note: this is iOS 7).
Each cell is a bit complicated, consisting of a UIView with a few labels. In other words, I want to set up a prototype cell by customizing the content view of the cell itself.
Here are the steps I tried:
Drag a UIView into the Table View in Storyboard and set it's tag to be 1.
Added a single label to that UIView (ultimately, I will need several labels).
Implemented some basic table view methods:
As follows:
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIView *view = (UIView *)[cell viewWithTag:1];
NSLog(#"%#",view); //this just prints "(null)" a bunch of times
return cell;
}
Here's the hierarchy of my GUI elements.
Here is a demonstration that I have set the tag of the UIView to 1.
And here is the UITableView:
It prints out (null) many times. What is the proper way to do this? I haven't had success finding any tutorials for complex custom UITableViewCells that are built through storyboard.
I think you need to drag and drop UITableViewCell to your UITableView not UIView.
Here is the screenshot:
I have a custom class that extends UITableViewCell. It has two labels and a UISegmentedControl.
Here is the cellForRowAtIndexPath() that I configured. When I inspect "cell" in the debugger it has all the data I'm providing. But somehow that data never gets applied.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
CustomGameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyData *my_data = [rows objectAtIndex:indexPath.row];
UILabel *my_date = [[UILabel alloc] init];
my_date.text = my_data.myDate;
[cell setMyDateLabel:my_date];
UILabel *my_question = [[UILabel alloc] init];
my_question.text = my.question;
[cell setMyQuestionLabel:my_question];
UISegmentedControl *my_choices = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:my.firstChoice, my.secondChoice, nil]];
[my_choices setSelectedSegmentIndex:my.choice];
[cell setMyChoiceSegments:my_choices];
return cell
}
The data that I want displayed is currently in an array I create in viewDidLoad() which is accessible to cellForRowAtIndexPath() through the "rows" var.
When I run the code in the simulator I get three rows in the table representing the three elements in the array I created in viewDidLoad(). However, the content of those rows look exactly like what I defined in the storyboard.
What am I missing?
Where are you defining the layout of your cell? In a NIB? In your storyboard? Programmatically in your initWithStyle of CustomGameCell? The implementation details vary a little based upon which approach you use, but you definitely need to either define NIB or prototype cell in your storyboard, or programmatically create the controls, set their frames, perform addSubview so they're included in the cell, etc.
Your code is adding new UILabel objects, not adding them as a subview to anything, doing it regardless if you're using a dequeued cell or not, etc. So there are numerous problems here. To see examples of how you might properly use a custom cell, see Customizing Cells in the Table View Programming Guide. But, like I said, the details vary a bit based upon how you're designing your subclassed UITableViewCell layout, so I hesitate to propose any code until you specify how you're designing your user interface.
You must have added the labels and segmentcontrol in the cells content view of the cell, if not then please do so.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
CustomGameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyData *my_data = [rows objectAtIndex:indexPath.row];
cell.myDateLabel.text = my_data.myDate;
cell.myQuestionLabel.text = my.question;
[cell.myChoiceSegments setSelectedSegmentIndex:my.choice];
[cell autorelease];
return cell
}
Also use autorelease for the memory management.
This is a weird thing I have been noticing lately. I am not able to see the accessory Type for my UITableView Cell even after setting the accessoryType with the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = #"Move to a New Location";
}else{
cell.textLabel.text = [self.locArray objectAtIndex:indexPath.row];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
It doesn't matter what accessory type I set. It always displays just the text label in the cell but, no button. Am I missing something? Note that this is not the first time I am using a UITableViewCell. I have never come across this problem earlier. Meanwhile I am testing this on iOS6 and have confirmed this behavior both on the iPad simulator as well as the device it self.
Well! I have solved this. My Viewcontroller was initially a subClass of UIViewController instead of UITableViewController. I did this thinking I would be adding extra views to the main View which would have nothing to do with the TableView. I then changed it to UITableViewController and voila it all worked perfect. But then this is a trade off between the flexibility provided by UiViewController and UitableViewController as I now have to add those extra views either in to the headerview or the footerview of the TableView. Nonetheless I have to deal with it any way. I hope this would help some one.
this is Nsr (just a beginner in xcoding), using xcode 4.3.3,
I've made a Custom UITableview with Custom UITableviewcell through storyboarding,
I have a UIBUtton and a UILabel in my custom cell.
i'v remove the cell selection (also cleared the background) so that only the buttons can be accessable which works as a backgound for the custom UILabel.
Now there are bunch of buttons since of using data array, and when i click any button, it segues to the other view (detail view), all i wanted is to set the custom Label (set over the detail view) from the previous selected button with "Label"...
means new Label = previous page's clicked Label from the custom tableview..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Custom *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[Custom alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.myLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.myTable reloadData];
}
I hope you get my problem, very sorry for my poor english..
Please help me, im in a real mess, coz this problem already took my 3 days :(
Thanx in advance, Cheers.
function return Custom cell but in header you write UItableviewCell change this to Custom
I was just playing around with IOS and I hit a problem and it seems so simple that I can't believe it's not working
I have a table list
I have set up all the delegate so that it displays information from a plist
that works
however when I switched to my custom view for the internal layout, I get the default text of the view but not the text form the plist
Here is some code of the table view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DefaultCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"DefaultCell"];
}
EmergencyContactItemConrtroller *genericViewController = [[EmergencyContactItemConrtroller alloc] initWithNibName:#"EmergencyContactItemConrtroller" bundle:nil];
[genericViewController setInitialParametersWithTitle:[[emergencyArray objectAtIndex:indexPath.row]objectForKey:#"Title"]
withCatption:[[emergencyArray objectAtIndex:indexPath.row]objectForKey:#"Caption"]
withNumber:[[emergencyArray objectAtIndex:indexPath.row]objectForKey:#"Number"]
withOtherInfo:nil];
[cell.contentView addSubview:genericViewController.view];
return cell;
}
and here is the code of setInitialParametersWithTitle in the cell view controller.
-(void) setInitialParametersWithTitle:(NSString *)title
withCatption:(NSString *)caption
withNumber:(NSString *)number
withOtherInfo:(NSDictionary *)otherInfo{
self.titleLable.text = title;
self.captionLable.text = caption;
[self.numberButton setTitle:number forState:UIControlStateNormal];
}
I checked that the xib IBOutlet are linked correctly.
when I try to debug I get he right values send to the function, how ever when I printout self.titleLable.text before assigning a value to it, there is no value in it.
Any Ideas ?
Jason
EmergencyContactItemConrtroller is UIViewController class?
All IBOutlets will initialized after
[cell.contentView addSubview:genericViewController.view]
is done; Then you should change them
Try to reload your table view in -(void)viewwillappear()
Try this, inside if(cell==nil),
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"DefaultCell"];
cell.contentView = [[UIView alloc]init];
}
Then you can add subView above cell's contentView