Tap gestures on an element in a UITableView custom cell - ios

This topic has been covered over and over, but after two days of researching and trying all of the solutions suggested, I still can't do what I want.
First of all, I'm creating an app for iOS 5, using storyboard.
I have a UITableViewController, with 2 types of cell (an original "message", and a number of "answers" to it). I created my table in my storyboard, checked "prototypes cells", designed 2 cells with my 2 or 3 labels, a textView, and an image. Then, I subclassed UITableViewCell with 2 new classes, which I called ThreadAlertCell and ThreadAnswerCell. I created properties for my cell's elements, so I can set the text of the labels and the image programmatically. I linked my graphic elements to their definition in the storyboard as usual. In my TableViewController, in cellForRowAtIndexPath, I create the cells and populate them. So far so good, everything is displayed correctly and how I want it.
But, I want a "touch" on the image of a cell, to pop a new view, showing the user's profile page (an other basic view, I can do it with performSegue, no problem for that).
I have tried so many things I'm not sure it's very useful to put everything in detail here. While looking for answers, I understood that using a UIImageView when you expect to handle gestures is not really the best way. So I changed it to a UIButton. But I can't get the touch event to do anything !
I'll give only the example of an "answer" cell.
Here is my ThreadAnswerCell header file (I won't give the .m, nothing interesting there) :
#interface ThreadAnswerCell : UITableViewCell
#property (nonatomic) IBOutlet UILabel *senderLabel;
#property (nonatomic) IBOutlet UILabel *dateLabel;
#property (nonatomic) IBOutlet UITextView *contentTextView;
#property (nonatomic, weak) IBOutlet UIButton *senderButton;
#end
And here is half the cellForRowAtIndexPath from my TableViewController (I do the same for the "message" before that) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ThreadAnswerCell";
ThreadAnswerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ThreadAnswerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Message *message = [threadArray objectAtIndex:indexPath.row];
cell.senderLabel.text = [NSString stringWithFormat:#"%#", message.sender];
cell.contentTextView.text = [NSString stringWithFormat:#"%#", message.content];
cell.dateLabel.text = [NSString stringWithFormat:#"%#", message.date];
//[cell.senderButton setBackgroundImage: [[UIImage alloc] initWithData: [NSData dataWithBase64EncodedString: message.userPic]]
// forState: UIControlStateNormal];
[cell.senderButton addTarget:self action:#selector(firstButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
CGRect frame = cell.contentTextView.frame;
frame.size.height = cell.contentTextView.contentSize.height;
cell.contentTextView.frame = frame;
[cell.contentTextView sizeToFit];
return cell;
}
As you can see, I use my custom cell, then populate it with content (I use three useless stringWithFormat but I have my reasons, lol), and I try to add an event to my button. I also commented the part where I set the button's background image to "see" the button on my screen.
And here is the method I want the buttons to call :
- (void)firstButtonSelected: (id)sender
{
NSLog(#"hello");
}
But, the method is never called ! Any ideas on where I've gone wrong or any other working solution would be great ! Thanks.

Do you have
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Implemented in your tableview delegate class?
I think, your cell catches the touch events. Try to delete the method. And
cell.selectionStyle = UITableViewCellSelectionStyleNone

Is it the topmost view? Make sure you add the senderButton view last to the cell view.
It's possible any other view gets touched.

Related

UITableView Slow to load

I have an UIViewController with an UITableView inside him, I draw the items inside my Table using the Interface (Drag and drop Labels, and UITextViews) my table have 25 rows.
Now it's time to link my labels with IBOutlets for this I create a Subclass of TableViewCell Like this:
TableViewCell.h
#property (nonatomic, retain) IBOutlet UILabel *label1;
TableViewCell.m
#synthesize label1 = _label1;
And I use this code for change my first label:
- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
HobbiesTableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[HobbiesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.label1.text = #"GG";
return cell;
}
But I saw a little problem, When I open my View (in simulator) is too slow to open (I think my table is loading), for try to solve this problem I delete the nonatomic of my property and my View open faster.
But I have one question, When I create Properties I always put the command nonatomic, and in this project I had to take it off because the slowness, have a problem to take off the nonatomic in this property?(Since the labels will never be changed!)
You should make label1 weak, as it is is in Storyboard. You also don't need to #synthesize anymore.
Also, if you are dequeuing with different identifiers, you are really not dequeueing. Prototype cells should have one identifier in storyboard.
NSString *CellIdentifier = #"MyCellID"
HobbiesTableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Nonatomic should increase performance, and is not causing your problem.
From what you have listed, I would rather blame not the table view, but the way you fill menuItems. Does its contents come from the internet or a file? If yes, I guess the problem is that you perform access to a remote data source on the main thread.
UPD:
After a short talk with the PO it became clear the the issue was because of having 25 cells with different Reuse ID's.

Outlets cannot be connected to repeating content iOS 5

I am new to iOS i am working on uitableview cell i had drag and drop the button to table view cell from objects inspector. but when i am making its IBoutlet it is showing an error like "Outlets cannot be connected to repeating content" what does it means?? we cant make outlets of UIbutton in tableview cell.kindly review it . i am stuck in it.i am making an outlet like this:
#property (weak, nonatomic) IBOutlet UIButton *sa;
and the error is "The sa outlet to the UIbutton is invalid"
Your answer is that you use an object in UITableViewCell by reusing it,
So you can't create it's outlet except you create Your class for your "UITableViewCell".
Below is a reference for you,
I hope that it's useful for you.
You need to drag and drop the object in your UITableViewCell,
Then you have to give tag for that object.
then you can use it with its tag.
First give an identifier to UITableViewCell,
Below is a reference image for it.
Then give a tag to your UI object,
As this in this reference image.
Below is sample code for you which I use regularly,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *Cell = [self.TableListRegion dequeueReusableCellWithIdentifier:#"List"];
UIButton *objectOfButton = (UIButton *)[CellLast viewWithTag:200];
[objectOfButton addTarget:self action:#selector(YourSelector:) forControlEvents:UIControlEventTouchUpInside];
return Cell;
}
Now you can receive that button event by,
-(IBACTION)YourSelector:(id)sender{
// Your Button in Cell is selected.
// Do your stuff.
}
Feel free to ask if you need more help regarding it.
You can create subclass for UITableViewCell like below code
Create new class named CCell.h and CCell.m
in CCell.h
#interface CCell : UITableViewCell
#property(nonatomic,strong)IBOutlet UILabel *lblTemp;
#property(nonatomic,strong)IBOutlet UIButton *btnTemp;
#end
Now in your Viewcontroller.h
#import "CCell.h"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CCell *cell = (CCell *)[tblView dequeueReusableCellWithIdentifier:#"CCell"];
cell.lblTemp.text = #"asd";
cell.btnTemp.tag = indexPath.row;
[cell.btnTemp addTarget:self action:#selector(btnTempClicked:) forControlEvents:UIControlEventTouchUpInside]
return cell;
}
-(void)btnTempClicked:(UIButton *)btnTemp
{
NSLog(#"Button Clicked Index = %d",btnTemp.tag);
}
Now open your Xib > tap on your UITableviewCell > open right side navigator > open 3rd tab named (Custom Class) > add Class = CCell > now open last tab you will get lblTemp bind option.
Maybe this will help you.
Since you are creating a custom cell, you need to create a class for it. You will subclass UITableViewCell.
For example (using the property that you had in your question):
Create a new Objective-C Class. Set the subclass to: UITableViewCell
Give it an appropriate name (i.e. cell)
In your cell.h file:
Create your property: #property (weak, nonatomic) IBOutlet UIButton *sa;
In the cell.m file:
#synthesize sa = _sa;
- (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
}
3.In Interface Builder, go back to the row that you created.
Click the "Show Identity Inspector".
Under "Custom Class", set that to your cell file.
4.Hold down the "Option" key and click the "cell.h" file. Then connect the button to the IBOutlet.
5.In your table view controller file:
import your cell.h file.
In cellForRowAtIndexPath method:
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
That's it!
Everything should work then. Hope this helps!
You can't, because the button is part of a cell, and there will (possibly) be multiple instances of that cell when the app runs.
Assume that you can make the connection, and there are 2 cells (and thus 2 buttons) when the app runs, Cocoa Touch can't decide which button will be referenced by that only outlet (neither can you).
You can subclass the UITableViewCell, ask the table view to use your subclass for its cells, and connect the button to the outlet in the subclass. In that case there will be multiple instances of the subclass, and each instance will map to one cell.
Whenever you need want to create custom tableView cell it is recommended to subclass UITableViewcell and add UIButton in that class. And in your tableview delegate method cellForRowAtIndexPath you can create an object of subclassed tableviewCell for every cell.
I have done in UITableviewcell cell's label's outlet connection in UIViewController,it should changed to Create a CustomCell in Subclass of UITableviewcell,then done in outlet connection in this subclass that error cleared.
#Mishal Awan
If you really want to do that and your have a finite number of cells. U can :
Changing your ViewController to be a subclass of UITableViewController, then drage a Table View Controller file to your StoryBoard
Changing the content of Table View in your StoryBoard from Dynamic Prototypes to Static Cells
Then you can add some views to your cell, for example some labels
Connectting the label to your ViewController and continue the remaining work
If you want to create a reusable cell, forget what i have said above
A very simple solution is:
Just take the view or NSLayoutConstraint reference outlets in the subclass of table view cell instead of table view controller and access using object of table view cell in cellForRowAtIndexPath method or any other method.
Note: This is a repetitive object so you can't take reference in table view controller.

Custom TableViewCell not displaying label

I'm struggling with a problem I encountered while trying to create a custom UITableViewCell.
I subclassed UITableViewCell in SGTableViewCell and added it in the prototype cell in the storyboard.
As you can see the label is connected
and the cell identifier is set correctly
Then I linked the label to the SGTableViewCell.h like this
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
and in the .m file I have this code
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self addGestureRecognizer:recognizer];
self.nameLabel = [[UILabel alloc] init];
_checkView = [[UIView alloc] initWithFrame:CGRectNull];
_checkView.backgroundColor = kGreen;
_checkView.alpha = 0.0;
[self addSubview:_checkView];
self.nameLabel.text = #"Hello";
}
return self;
}
But when I use this cell in my tableview using this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Episode *episode = [self.selectedSeason.episodeList objectAtIndex:indexPath.row];
SGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Episode"];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = kSelectionGrey;
cell.selectedBackgroundView = selectionColor;
cell.backgroundColor = kBackgroundGrey;
cell.nameLabel.text = episode.name;
NSLog(#"%#", cell.nameLabel.text);
cell.nameLabel.textColor = [UIColor redColor];
return cell;
}
I get no text at all.
I tried logging the text from each label in each cell and it gives me the right text.
I tried setting programmatically a different disclosure indicator for the custom cell and it did change so everything is allocated and working but label is not displaying.
I honestly have no idea of what's the problem. Did I miss something?
Thank you
PARTIALLY SOLVED:
OK i tried doing the same thing on an empty project and everything worked flawlessly so I checked again my project and found this line
[self.tableView registerClass:[SGTableViewCell class] forCellReuseIdentifier:#"Episode"];
Seeing it was not necessary for the empty project i commented this line and everything started working.
The only problem i have now is that if i don't use this line i can't use the custom cell as was intended. In fact my custom cell is swipable using a pan gesture recognizer but without registering my custom class to the tableview seems like the swipe doesn't work.
Sorry for the trouble, seems like i messed up again :/
You shouldn't alloc init a label that you created in the storyboard, it is already allocated automatically. When you do self.nameLabel = [[UILabel alloc] init];, you reset the self.nameLabel property to point to a new empty memory location and not to the label created in the storyboard, hence you can change its text property and see the result in NSLog but not in the storyboard because it doesn't refer to that label in the storyboard.
Try removing all initialisation from the initWithStyle method (to make sure nothing is covering it such as that subview you create), and everything related to the label in the cellForRowAtIndexPath method (same reason), and try a simple assignment like self.nameLabel.text = #"Test text" in the cellForRowAtIndexPath method, it should work. Then add all your other initialisation.
And yeah, don't forget to input your cell reuse identifier "Episode" in the storyboard.
Make sure you:
Have linked the delegate and the datasource to the view the tablview is housed in.
Have that view implement UITableViewController and UITableViewDelegate (I'm pretty sure it is both of those).
Implement the necessary methods, which you seem to have done. You need the row size, section size, and the add cell methods
After updating the array linked to your tableview, call [tableView reloadData]
Have a look at this link:Tutorial to create a simple tableview app

If a UITableViewCell has multiple buttons, where is the best place to handle touch events

I'm presenting a lot of data in format of a table with multiple columns. Almost each column has a button (up to 4 in total) and each row is a UITableViewCell.
How could I detect that the buttons were touched and where should I handle touch events of the buttons? I'm certain, it shouldn't be a didSelectRowAtIndexPath method though.
As soon as, I could detect which button was pressed, I would fetch the data in that particular row and manipulate it. So, I need to know the indexPath of the row, as well as what button was pressed on it.
You can subclass UIButton with two properties row and column and implement the logic below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"
forIndexPath:indexPath];
MyButton *button1 = (MyButton *)[cell viewWithTag:1];
button1.row = indexPath.row;
button1.column = 1; // view tag
[button1 addTarget:self
action:#selector(clickAction:)
forControlEvents:UIControlEventTouchUpInside];
// button2,button3...
return cell;
}
-(void)clickAction:(MyButton *)sender {
// now you can known which button
NSLog(#"%ld %ld", (long)sender.row, (long)sender.column);
}
Generalized undetailed answer:
Create UITableviewcell subclass, link cell ui elements to this class.
Add method configureWithModel:(Model*)model; //Model being the information you want the cell to represent
Manipulate that information or
If you need to manipulate the screen or other objects. You need to give the table view cell subclass a reference to the other objects when the cell is created. (in code or in storyboard or in nib).
how to handle button presses in ios 7: Button in UITableViewCell not responding under ios 7 (set table cell selection to none)
how to link a button: http://oleb.net/blog/2011/06/creating-outlets-and-actions-via-drag-and-drop-in-xcode-4/
If those four views are UIButton then you will receive the tap events on each button or if they are not UIButton then you should add UITapGestureReconiser on each of this views
Several options here. But I would do the following:
Adopt a Delegate Protocol in your custom cell class (see here: How to declare events and delegates in Objective-C?) . This will handle the target selector for the buttons. Pass this message back to your view controller with the sender. To detect which cell it was in do the following:
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
CGRect senderFrame = CGRectMake(buttonPosition.x, buttonPosition.y, sender.frame.size.width, sender.frame.size.height);
From here you can decide what the do. Use the buttons .x coordinate to determine which button it was or specify a different tag for each button in cellForRowAtIndexPath:. Or if you want to grab the index path of the cell you can do:
NSArray *indexPaths = [YOUR_TABLE_VIEW indexPathsForRowsInRect:senderFrame];
NSIndexPath *currentIndexPath = [indexPaths lastObject];
Because each button has a different action, the only thing you need to get at runtime is the indexPath of the button. That can be done by looking at the button's superviews until a cell is found.
- (IBAction)action1:(UIButton *)button
{
UITableViewCell *cell = [self cellContainingView:button];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
MyDataModel *object = self.objects[indexPath.row];
// perform action1 on the data model object
// Now that the data model behind indexPath.row was done, reload the cell
[self.tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation: UITableViewRowAnimationAutomatic];
}
- (id)cellContainingView:(UIView *)view
{
if (view == nil)
return nil;
if ([view isKindOfClass:[UITableViewCell class]])
return view;
return [self cellContainingView:view.superview];
}
There: no delegates, no tags, and the action doesn't care about the internals of the cell.
You will still want to subclass UITableViewCell with the four buttons (call them button1, button2, button3, and button4 if you don't have better names). You can make all the connection is Interface Builder. This will only be needed for populating object data into the cell during -tableView:cellForRowAtIndexPath:.
Ideally, you should create a custom cell by subclassing UITableViewCell and implement the actions for each of these buttons in that cell. If your view controller needs to know about these actions, you can define a protocol, MyCustomCellDelegate or similar, and have your view controller conformed to that protocol. Then MyCustomCell will be able to send messages to the view controller when user interacts with its buttons or other controls.
As in the example code below, you can create a cell in storyboard or nib and hook one of the button's action to firstButtonAction method of CustomTableCell class.
Also, you need to set your view controller as delegate property of CustomTableCell object created and implement the method buttonActionAtIndex: of CustomTableCellDelegate in your view controller class. Use controlIndexInCell param passed to this method to determine which button might have generated the action.
#protocol CustomTableCellDelegate <NSObject>
- (void) buttonActionAtIndex:(NSInteger)controlIndexInCell
#end
In CustomTableCell.h class
#interface CustomTableCell: UITableViewCell
#property (nonatomic, weak) id <CustomTableCellDelegate> delegate
- (IBAction) firstButtonAction:(id)sender
#end
In CustomTableCell.m class
#implementation CustomTableCell
#synthesize delegate
- (IBAction) firstButtonAction:(id)sender{
if ([delegate respondToSelector:#selector(buttonActionAtIndex:)])
[delegate buttonActionAtIndex:0];
}
#end
This is a personal preference on how I like to handle situations like these, but I would first subclass UITableViewCell because your table cells do not look like a default iOS UITableViewCell. Basically you have a custom set up, so you need a custom class.
From there you should set up your 4 IBActions in your header file
- (IBAction)touchFirstButton;
- (IBAction)touchSecondButton;
- (IBAction)touchThirdButton;
- (IBAction)touchFourthButton;
You do not need to pass a sender in these actions, because you will not be using that object in these methods. They are being created to forward the call.
After that set up a protocol for your UITableViewSubClass
#protocol UITableViewSubClassDelegate;
Remember to put that outside and before the #interface declaration
Give your sell a delegate property
#property (nonatomic, strong) id<UITableViewSubClassDelegate> delegate;
and finally define your actual protocol, you will need to set up 4 methods, 1 for each button and take your subclass as a parameter
#protocol UITableViewSubClassDelegate <NSObject>
- (void)forwardedFirstButtonWithCell:(UITableViewSubClass*)cell;
- (void)forwardedSecondButtonWithCell:(UITableViewSubClass*)cell;
- (void)forwardedThirdButtonWithCell:(UITableViewSubClass*)cell;
- (void)forwardedFourthButtonWithCell:(UITableViewSubClass*)cell;
#end
This will be placed outside of the #interface #end section at the bottom
After that create a configureWithModel: method in your #interface and #implementation as well as a property for your model
#interface:
#property (nonatomic, strong) Model *model;
- (void)configureWithModal:(Model*)model;
#implementation:
- (void)configureWithModal:(Model*)model {
self.model = model;
// custom UI set up
}
From here you should configure your action methods in your #implementation file to call the delegate methods, i'm only showing the first one, but you would do this with all of the IBActions
- (void)configureWithModal:(Model*)model {
[self.delegate forwardFirstButtonWithCell:self];
}
From here your custom cell set up is done and we need to go back to the UIViewController that is displaying the UITableView. First go into the header file of the view controller, and import your custom UITableViewCellSubClass and then setup the class to implement this protocol.
It should look something like this
#interface MYViewController : UIViewController <UITableViewSubClassDelegate>
from there you should go into your cellForRowAtIndexPath: method and configure your custom UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCellSubClass *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.delegate = self;
Model *cellModel = self.tableData[indexPath.row];
[cell configureWithModel:cellModel];
return cell;
}
Now go into your cell class and copy paste all of the protocol methods into your viewController class. I will display one as an example.
In your UIViewController:
- (void)forwardedFirstButtonWithCell:(UITableViewSubClass*)cell {
Model *cellModel = cell.model;
// do stuff with model from outside of the cell
}
do that for all methods and you should be good.
Remember to have all your #imports in so there's no forward declarations and remember to link up the IBActions to your storyboard or xib files. If you want a custom xib for your table cell you will have to check if the cell is nil and then allocate a new one, but if you are using prototype cells then this should be sufficient.
For simplicity sakes i put forwardFirstButtonWithCell: but i would encourage making the name something that describes what it's doing such as, displayPopOverToEnterData or something similar. From there you could even change the parameters of the delegate protocol methods to take models instead so instead of
- (void) displayPopOverToEnterDataWithCell:(UITableViewSubClass*)cell;
make it
- (void) displayPopOverToEnterDataWithModel:(Model*)model;
but, i don't know what type of information you need to access from the cell. So update these methods as you see fit.

UITableView not displaying content in UIViewController

I have added a UITableView inside a UIViewController in IB, I have set the TableView content to "Static Cells" since that's what I want, and everything seems fine when I haven't added any content to the UITableView. But if I for example change the first UITableViewCell's background color to black it doesn't display black when running the app. I have added UITableViewControllerDelegate and UITableViewDataSource and set the tableView.delage = self;. But still no changes I make to the tableView displays when I run the app.
What can the problem be?
NOTE: I have also tried to add tableView.dataSource = self;, but that just make the app crash.
Yes, you can have static table view content in UIViewController.
All you need to do is:
-Create the table's static cells in interface builder and design them the way you like.
-Make the UIViewController implement table view's data source and delegate:
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
-Connect the table view's delegate and dataSource to the view controller in interface builder
-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of your cells. (e.g. return 10, yes simple as that)
-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT: Make sure they are strong, weak won't work. e.g. #property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;
-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return the correct cell at index path. e.g:
int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
case 0:
cell = self.myFirstCell;
break;
case 1:
cell = self.mySecondCell;
break;
}
return cell;
If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info check here: Best approach to add Static-TableView-Cells to a UIViewcontroller?
You will want to use a UITableViewController, not a UIViewController with a UITableView added to it, because you're only supposed to use static cells with a UITableViewController. There are probably ways to hack around it so you can get the static cells to work, but it's much simpler to just use a UITableViewController, and you'll have fewer issues to deal with, especially if you ever change the content of the table.
Seems you have problem with the background issue for UITableViewCell. So don't use background for checking if content is drawing or not.
You can use debugger for this for example or NSLog.
NOTE: the cell has content view that can be modified. I don't remember but seems the cell has not got background property that can be adjusted with a color.
If you tried this line below e.g. - it will no work and color will be white as default color.
[cell setBackgroundColor:[UIColor blackColor]];
Try to add something to the cell for example picture and then you can see the result as I think.
Use this code:
[cell.contentView setBackgroundColor:[UIColor blackColor]]; in this delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
it will help you as I think.
Have you implementede the protocol? ...
another thing is that when implementing the protocol i had an issue when no cell was displayed..
try with this implementation for the given method.
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier=#"Cell";
CobranzaCell *cell = [[CobranzaCell alloc]init];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil) {
cell = [[CobranzaCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
return cell;
}
You cannot use the static cells in simple UIViewController subclass. Instead, you should use the instance of UITableViewController. The hack is in your storyboard drag the instance of UIViewController to your storyboard canvas. Then, drag the Container View from objects library and drop it to your UIViewController's view. By default it will create the embed segue with related UIViewController instance. What you want to do - delete this view controller, drag and drop instance of UITableViewController from objects library, then right click and drag from your Container View to just dropped UITableViewController. Chose embed. Now your table view controller gets the size of container view and you can use static cells in it! Hope it will help!

Resources