iOS Storyboard - creating customized UITableViewCell - ios

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:

Related

Custom prototype cell in UIStoryboard is created but doesn't show up in UITableView

I have a project where I need to use a custom UITableViewCell. I'm designing the cell as a prototype in storyboard and it looks fine there. I assign the prototype to my custom UITableViewCell subclass, give it the same reuse identifier I'm using in my UITableView and link the UILabel on the prototype cell to an IBOutlet in my UITableViewCell subclass.
When I call it from the UITableView the cell is created and if I add labels and buttons in the code of that class (create them with a CGRect and all) they all work but the labels I've added in the storyboard never show up.
I don't understand how my subclass can be called and created successfully but its layout and subviews from the storyboard don't seem to exist as far as my app is concerned. What am I doing wrong?
Here's my cellForRowAtIndexPath code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
I've run into this issue before, and in my case, the problem was that the auto-generated code for the view controller included a call to:
[UITableView registerClass:forCellReuseIdentifier:]
I would suggest checking for and removing any calls to the above, or to
[UITableView registerNib:forCellReuseIdentifier:]
and trying your original code again.
acreichman, add casting in cellForRow and put an NSLog in you cell's awakeFromNib to see if you get there. Let me know...
Your cellForIndexViewPath should look like this, to create a simple custom cell with label,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Make sure that you have made all connections well, set datasource and delegate to table and then set the “Identifier” of the custom cell to "MyTableViewCell" in “Attributes Inspector” like this,
For storyboard:
Add "MyTableViewCell" instead of "SimpleTableCell" as shown in above screenshot.

Programmatically add content to static table view cell in IOS 6

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

Why does my custom UITableViewCell never change from the prototype?

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.

Error while adding UICollectionView inside UITableViewCell

So I am trying to display horizontally scrollable collection view inside tableviewcell. the code i am using is
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UICollectionViewController *cv = [self.storyboard instantiateViewControllerWithIdentifier:#"collectionViewID"];
cv.view.frame = cell.contentView.bounds;
[self addChildViewController:cv];
[cell.contentView addSubview:cv.view];
[cv didMoveToParentViewController:self];
return cell;
}
I am getting error: Object can not be nil. I'd appreciate if someone can help me out understanding the error.
I have done this in my app.
I found it much easier to subclass UITableViewCell. The I could put all the UICollectionView setup and the UICollectionView datasource and delegate inside the code for the cell.
I then provided one public property of type NSArray which I pass into the cell. The cell then uses this array as the datasource for the UICollectionView that it owns itself.
Made it a lot easier to manage.
check your UICollectionViewControlle's attribute inspector for identifier value {collectionViewID}
EDIT
[self addChildViewController:cv]; --> I think you want to add cv to the cell, isn't you?

Cannot access the Custom UILabel in a Custom UITableViewCell

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

Resources