I need a grouped UITableView similar to the one for Twitter accounts in Settings app:
That is, a sort of form or menu where some of the sections have a beforehand known set of static cells, and some other sections have to be dynamic and allow inserting additional rows the same way the "Add Account" does here. I'm managing the UITableView in a .xib file. For the static cells, I have separated .xib files that I can load within the cellForRowAtIndexPath: method in the view controller.
How should I handle this kind of table? I donĀ“t find any example code.
How the cellForRowAtIndexPath: method should look like? May I need to keep strong properties for the static cells? Would it be better to design each static cell directly within the same .xib file where the table view is, and to set outlets for them? (Though this does not allow to reuse my custom cells design...)
I need some guidelines for achieving this and correctly managing cells and memory. Thanks in advance
Dynamic prototype cells can behave like static ones if you just return the cell without adding any content in cellForRowAtIndexPath, so you can have both "static like" cells and dynamic ones (where the number of rows and the content are variable) by using dynamic prototypes.
In the example below, I started with a table view controller in IB (with a grouped table view), and changed the number of dynamic prototype cells to 3. I adjusted the size of the first cell to 80, and added a UIImageView and two labels. The middle cell is a Basic style cell, and the last one is another custom cell with a single centered label. I gave them each their own identifier. This is what it looks like in IB:
Then in code, I did this:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1)
return self.theData.count;
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 80;
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"TitleCell" forIndexPath:indexPath];
}else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:#"DataCell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
}else if (indexPath.section == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:#"ButtonCell" forIndexPath:indexPath];
}
return cell;
}
As you can see, for the "static like" cells, I just return the cell with the correct identifier, and I get exactly what I set up in IB. The result at runtime will look like your posted image with three sections.
Static is just a provision on top of dynamic layout. Basically static is a WYSIWYG.
If you are not resistant to experiment with values, I would recommend go dynamic. There are hundreds of examples available, such as this and this.
As you go further, you would see yourself diverging towards two options when customizing dynamic table views:
Subclass UITableViewCell (more effort , but good in the long run). Again, follow this.
Play with UITableViewCell properties inside cellForRowAtIndexPath: (less effort and quick result but may or may not be performance-friendly due to possible redrawing)
The possibilities are endless, such as here where cell background view is customized.
Related
In this project I have absolutely no code. In my main.storyboard I have a TableViewController with so far 1 cell. In that one cell I put an Image View in, then I selected the certain picture. The cell is 320 (Width) by 44 (Height). Everything seems fine in the main.storyboard, however I press run, the app opens but there is nothing there, only a bunch of lines.
Any help would be great I will give screenshots to anyone who needs them in order to help me solve this problem. I am using xCode 5.1
If you don't want to write any code than you must tableView type to static!
Check that.
Here's the snippet for TableViewDelegate. Hope this will help. Don't forget to thumbs up
Make sure you have this in your TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1; ---->> should return 1
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10; ----->> return how many rows you want to show, mostly you write
return yourarray.count;
}
I think the problem will be in the method below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil; ---->> Instead of UITableViewCell you need to
change to the class name of the
TableCell ( This is where your cell is
created)
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Cell"]; ---->>Instead of UITableViewCell you need to
change to the class name of the
TableCell ( This is where your cell is created)
}
cell.textLabel.text = #"Cell";
return cell;
}
You have to use either a static tableview that uses predefined cells or (by default) use a dynamic tableview - in that case you have to provide the cells in code by adhering to the tableviews data source protocol. Which you seem to not be doing now.
So, in interface builder, set the tableview to be static and you can build the cells in interface builder and they will be displayed. Make sure you delete the boilerplatecode in table viewcontroller that is given to you to easily implement the protocol (if you don't delete it, the default code returns nil off the cells and so none will get displayed).
When you get it working, find a nice tableview tutorial. I suggest ray wenderlich or the cs193p course on itunesU.
I'm trying to create detail view controller as a list of information and I think it would be nice and clean to present this with a static UITableView. But after that it came to my mind that on some level it might be difficult, so please resolve my doubts!
Every UITableViewCell has different style (some are custom, some are basic and few are right-detailed etc.).
What is more, content size of each cell may vary as I have long names put inside labels so they use autolayout to fit.
There is no problem when I have the same cells repeating but with different tex inside UILabels. In that case I use a simple:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.prototypeCell) {
self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:#"ActivityCell"];
}
[self fetchedResultsController:[self fetchedResultsController] configureCell:self.prototypeCell atIndexPath:indexPath];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
I don't know how to deal with heightForRowAtIndexPath. I can give an identifier to each cell, call cellForRowAtIndexPath:, and make a big switch or if statement, but is it right? The same problem occurs while I think of cellForRowAtIndexPath: and populating those UITableViewCells. With those testing statements this code won't be pretty and readable.
Any ideas on that case?
In the delegate function of the table view named heightForRowAtIndexPath try to calculate the height for each row and then return it.
//return height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==tblLanguage)
{
//Here calculate the dynamic height according to songs count for specific language
return (([[arrSongListForSpecificLanguage objectAtIndex:indexPath.row] count]*40)+40);
}
return 40.0;
}
I have grouped static cells in a UITableView. Now I'd like to add or delete (what is easier?) one specific cell, which I've already created in my storyboard. It depends on one NSString: If my string == YES, the cell should be displayed, else it shouldn't.
already tried tableView:insertRowsAtIndexPaths:withRowAnimation:
What you need is:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This method is part of the UITableViewDelegate protocol and will be called when the table view is about to draw a cell for a particular row. This provides a UITableViewCell for you to work with.
To identify which cell you need to hide, you can add a tag to the UITableViewCell in the storyboard. This is easier because you are adding static cells.
Then you can just do:
if ([cell tag] == someInteger) {
cell.hidden = YES;
}
You can wrap this in a condition based on the value of your string that you have mentioned.
I have added some example code on Github to illustrate this.
I am setting up my UITableView using storyboard editor. For creating my cells I am using the standard delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SearchResultCell"];
if (cell == nil)
{
// Do cell setup
}
// etc
return cell;
}
Except when the cell is dequeued the very first time it's not nil, as it should be. So the code inside the if statement is never executed.
People get this error when their reuse identifiers are inconsistent, so I went ahead and verified that I am using the exact same reuse identifier in my storyboard views as I do in my code. Still facing the issue. I also have several tableviews within the project and each one has a unique reuse identifier. Still no dice. Anyone know anything else that could be wrong here?
That's not how UITableView works anymore. Reading your question, I think you might be confused about how it worked before as well. If not, sorry, the first part of this is just review. :)
Without storyboard cell prototypes
Here's how it used to work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the tableview has an offscreen, unused cell of the right identifier
// it will return it.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SearchResultCell"];
if (cell == nil)
{
// Initial creation, nothing row specific.
}
// Per row setup here.
return cell;
}
Here when you create the cell using the reuse identifier, you do only the initial setup here. Nothing specific to this particular row/indexPath.
Where I've put the Per row setup comment you have a cell of the right identifier. It may be a fresh cell, or a recycled cell. You're responsible for all setup related to this particular row/indexPath.
Example: if you set the text in some rows (likely) you need to set or clear it in all rows, or text from rows you set will leak through to cells you don't.
With storyboard prototypes
With storyboards, though, the storyboard and table view handle the initial cell creation! This is brilliant stuff. You map out your cell prototypes directly in the tableview when using storyboards, and Cocoa Touch will do the initial creation for you.
Instead, you get this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SearchResultCell"];
// You'll always have a cell now!
// Per row setup here.
return cell;
}
You're responsible for all the same per row setup as before, but you shouldn't need to write code to build your initial empty cell, either inline or in its own subclass.
As Ian notes below, you can still use the old approach. Just make sure not to include a cell prototype in the storyboard for the identifier you specify. The view controller won't be able to build your cell from the cell prototype, dequeueReusableCellWithIdentifier will return nil, and you'll be exactly where you were before.
I need to do something like this :
.
Is it a UITableView? Or how should I do this? Any idea is welcome.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) return cell1;
} else if (indexPath.section == 1) {
if (indexPath.row == 0) return cell2;
} else if (indexPath.section == 2) {
if (indexPath.row == 0) return cell3;
}
}
Most likely its a UITableView. May be its not (it can be a UIScrollView with views added like sections in a table view).
But if you want to create something like in the image, I'd suggest you to use UITableView with customized cells. Because implementing table view, in this case, is simpler as you have to just concern about the delegate and the dateSource of the table view, rather than worrying about aligning the views in order.
Use sectioned table view with the translucent, gray-bordered, black-background-view as the backgroundView of the cells. Add the labels and arrow as the subviews of cell. You can add arrow as the accessoryView but that would become vertically centered, but in the image the arrow is displayed slightly on top of the cell.
There should be only one cell in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
There can be any number of sections (3 here).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3; // Can be any number
}
Yes, that's most likely a styled table view. See this guide or this nice tutorial.
why you worry about this one? Its simple man.... its nothing ,if you have hands over UITableView. Yeah, You must know about :
1: Set background image on view,exact as table size.
2: Set table view onto them and make background transparent.
3: Make customize table cell and add to table' row.
Thats it.....enjoy this work..its a creativity.
And you can do it guy....
I cannot tell exactly what is used from the picture, however I suggest using the UITableView especially if you have variable data coming from a structured object.
Some tutorials which I found very useful are the following:
Creating customized UITableViewCell from XIB
http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/
UIViewTable anti-patterns - A very good MVC pattern to build UIViewTable
http://www.slideshare.net/nullobject/uitableviewcontroller-antipatterns
And of course Apple's docs:
http:/developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
One important fact to remember is that you should always reuse cached table cells through the datasource method tableView:didSelectRowAtIndexPath:
Note: Since you want lots of transparencies there might be some performance issue especially on older devices.
Hope this helps and goodluck with your project.
There is not need to its scrollview or tableview... both are perform same..
Just change the Background Color of Tableview as clear color.....
check this code...
put this line to viewDidLoad :
[tableView setBackgroundColor:[UIColor clearColor]];
and
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
..... // Your label buttons etc........
......
......
.....
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setClipsToBounds:YES];
[[cell layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[cell layer] setCornerRadius:10];
}
You can do this using tableView in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//here place return your array contents
}
We can accomplish the similar as shown in image above by Table View and scroll view both.If you don't want to use table view then you can take a scroll view or a simple view and can add several buttons along with the image in background with different frames.It means button background will have the same image.then you can use different labels to show all the texts in different labels according to the requirement.
OR
you can use table view.For this you need to return number of sections 3(in this image) in the delegate method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.and then everything as the default tableview style.And then change the cell style to detailed.
yes this is a tableView with custom UITableViewCell.you can do that by creating a empty xib file and then add a UITableViewCell and do your customizing ...then in cellForRowAtIndexPath method use:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:edentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"yourCellName" owner:self options:nil]objectAtIndex:0];
this is from my application.
Yes, it is a groupedtableview with clearcolor as backgroundcolor. Cell background color also needs to be changed to clearcolor.
I suggest using div or list, don't use table, its very hard to control if the contents are too long.