I want to have the height of my UITableView cells depend on the material they contain. My view controller object is a subclass of UITableViewController and conforms to the <UITableViewDelegate> protocol. Is this the correct place to reference the protocol?
Then in my implementation I declare
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 20;
}
but when I run the app each row is of fixed height that is visually represented in the storyboard. Is it something with the prototype cell that is overriding my code? Am I missing something completely naive?
Did you set your view controller as delegate to table view. You can do it either through connection inspector of your table view in xib/ storyboard. Or you can do it by doing following in your initWithNibName:bundle: method
myTableView.delegate = self;
Related
How can i programatically add a UICollectionView in the UITableView's viewForHeaderInSection ?
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UICollectionView *cell;
return cell;
}
Note: I have already added the UICollectionViewDataSource and UICollectionViewDelegate in the .h file. But, i am not sure how to programatically create the UICollectionView . Can someone please help.
Maybe you want [[UICollectionView alloc] initWithFrame:(CGRect)];?
I don't know your class design, but maybe you can init the collection view(s) and set their delegate and datasource in viewdidload, store them in an array, and choose the collection view at the right index in viewForHeaderInSection.
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!
Here is what I have done:
I created a custom xib file that has a small UIView used for a custom table section header.
I classed the custom xib file.
I want to add this to a tableView as the header. I have looked at a few resources, but they seem to be either outdated or missing information.
Looking in the documentation, I see a reference to adding a custom header with the following instructions:
To make the table view aware of your header or footer view, you need to register it. You do this using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method of UITableView.
When I added a tableView to my storyboard view, it was easy to assign it a reuse identifier within XCode. I was even able to create a custom cell xib file and it also had a spot for a reuse identifier within XCode.
When I created the custom UIView for the section header, it did not have an entry for reuse identifier. Without this, I have no idea how to use registerNib:forCellReuseIdentifier.
More information:
I have a storyboard scene that has a tableView inside. The tableView is of a custom class that is linked and the tableView object has an outlet in the parent view's ViewController file.
The parent ViewController is both the UITableViewDataSourceDelegate and UITableViewDelegate. Again, I was able to implement the custom cells with no issue. I can't even modify the header in any way besides the title.
I tried calling the method [[self tableHeaderView] setBackgroundColor:[UIColor clearColor]]; from the custom tableView class and nothing happens. I tried using this method in the parent ViewController class by using the outlet name like so:
[[self.tableOutlet tableHeaderView] setBackgroundColor:[UIColor clearColor]];
Any help would be greatly appreciated.
EDIT: (Can't change background to transparent)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
HeaderView *headerView = [self.TableView dequeueReusableHeaderFooterViewWithIdentifier:#"tableHeader"];
// Set Background color
[[headerView contentView] setBackgroundColor:[UIColor clearColor]];
// Set Text
headerView.headerLabel.text = [self.sectionArray objectAtIndex:section];
return headerView;
}
You don't need to set the identifier in the xib -- you just need to use the same identifier when you register, and when you dequeue the header view. In the viewDidLoad method, I registered the view like this:
[self.tableView registerNib:[UINib nibWithNibName:#"Header1" bundle:nil] forHeaderFooterViewReuseIdentifier:#"header1"];
Then, in the delegate methods:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:#"header1"];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
On the problem with background color (Unless you want transparent):
You can create an UIView which occupies the whole view then change the background color of that.
If you don't want others to know what's happening, you can overwrite the backgroundColor property:
//interface
#property (copy, nonatomic) UIColor *backgroundColor;
//implementation
#dynamic backgroundColor;
- (void)setBackgroundColor:(UIColor *)backgroundColor {
//self.viewBackground is the created view
[self.viewBackground setBackgroundColor:backgroundColor];
}
- (UIColor *)backgroundColor {
return self.viewBackground.backgroundColor;
}
I am creating an App with following design:
I have a UITableView with custom UITableViewCell.
Each UITableViewCell contains a scroll view with multiple UIViews of size 150x150
Each of the small UIView contains a UITableView.
Since i am new to this, the forum is not allowing me to upload a picture, which could have explained the design very easily.
I have created the full design in the storyboard. The issue i am having is that the inner UITableView is not getting populated. The cellForRowAtIndexPath, numberOfSections or the numberOfRowsInSection methods of the datasource table view controller class do not get called.
I am using XCode 4.3.2 iOS5.
Following are the class structure:
NewsTableViewController : The ViewController for the mail table.
NewsTableViewCell : Custom TableViewCell which contains object of custom UIScrollView.
NewsScrollView: Custom UIScrollView containing UIViews and UITableView objects which are linked to the Storyboard UITableViews inside the UIViews.
NewsInnerTableViewController : TableViewController to be used as Datasource and Delegate for InnerTableViews.
I have created the object of NewsInnerTableViewController in NewsTableViewController and assigned it as delegate and datasource of the inner tableview.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSSTring *CellIdentifier = #"NewCell"
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
newsInnerTableController = [[NewsInnerTableViewController alloc] initWithStyle:UITableViewStylePlain];
cell.scrollView.innerTable.delegate = newsInnerTableController;
cell.scrollView.innerTable.datasource = newsInnerTableController;
return cell;
}
I am unable to understand where i am going wrong. or is something like this needs to be created is some other way. or the Datasource and delegate needs to be assigned at some other level.
Please share your thoughts on this.
the trick is to have the UITableTableViewCell as the Datasource and delegate for the inner UITableView
http://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-inside-a-uitableviewcell/
Before ios 5 I would set the row height in a tableview like this:
self.tableView.rowHeight=71;
However, it doesn't work on iOS5.
Someone has an idea?
Thanks
Have you tried tableView:heightForRowAtIndexPath: from UITableViewDelegate?
You may set the row height to 71 by implementing tableView:heightForRowAtIndexPath: in your UITableView delegate (one who supports the UITableViewDelegate protocol).
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 71.0;
}
First you should set a delegate of your tableView. Delegate should conform to the UITableViewDelegate protocol. Let's say we have a TableDelegate class. To conform to a UITableViewDelegate protocol one should have it in square brackets in it's declaration like this:
...
#interface TableDelegate : UIViewController <UITableViewDelegate>
...
or
#interface TableDelegate : UIViewController <some_other_protocol, UITableViewDelegate>
Then you set the delegate:
...
// create one first
TableDelegate* tableDelegate = [[TableDelegate alloc] init];
...
self.tableView.delegate = tableDelegate;
In the end you should implement the tableView:heightForRowAtIndexPath: method in the TableDelegate implementation:
#implementation TableDelegate
...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 71.0;
}
...
#end
rowHeight
Just to clarify, using rowHeight should work just fine and perform better than constant returned from -tableView:heightForRowAtIndexPath: as Javier Soto points out in the comments. Also note, that if your UITableView has delegate returning height in -tableView:heightForRowAtIndexPath: and rowHeight property set, prior value is honoured.
Im coding for iOS 5 and it actually works. You just need to implement the line you said in the:
- (void)viewDidLoad
method after the:
[super viewDidLoad];
The:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method does not work if the TableView is empty. But if you use the rowHeight property it will work even if the view is empty.
This method is change the Height of row
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return 51.0f
};
Try setting the rowHeight before viewWillAppear:, e.g. right after creating your table view.
This made it work for me on iOS 5. On iOS 6 it's easier: you can set it anywhere.
The advantage of using rowHeight, as pointed out by others, is that you avoid the performance impact of tableView:heightForRowAtIndexPath:.