Complex UITableView with Prototype Cells, Sections - ios

I'm looking for the best possible solution for a complex UITableView. What I want : I have an arbitrary number of sections. In this section is just one static cell and then should come any number of dynamic cells.
My Data are stored in some NSMutableArray and i tried somethink like this:
Combine static and prototype content in a table view
But i dont know how to handle it with my kind of problem.
So can somebody give me a hint or a best practice?
UPDATE: 1
http://jsfiddle.net/konstheinrich188/HKkA8/ Fiddle shows how my data looks and what im trying todo
This pic shows how i want to code my tableview...

When you are using the term "static", are you really talking about a "uitableview static cell", or rather "cell with static content"?
If the former; why do you need that?
If the latter; how about testing in each section whether this is the first cell in the section, and then presenting static content? For all other cells, display dynamic content:
if (indexPath.row == 0) {
// First row in section. Display static content
...
} else {
// Display dynamic content
...
}
For what purpose do you need the static cells?

So after a while and some testing and listen to your ideas and technicals i solved my problem!
Here is a little code :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mappedSprints count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
UISprint*s = [mappedSprints objectAtIndex:section];
return s._name;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
UISprint* s = [mappedSprints objectAtIndex:section];
return [s.internalUserStorey count] + [s.externalUserStorey count] + 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 130;
}
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"Cell%d%d", indexPath.row, indexPath.section]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:#"Cell%d%d", indexPath.row, indexPath.section]];
}
UISprint*s = [mappedSprints objectAtIndex:indexPath.section];
if (indexPath.row == 0) {
//Here is my first cell
//cell.textLabel.text =s._name;
}
else if(indexPath.row >= 0 && indexPath.row<=[s.internalUserStorey count]){
//here are the cells for SubItem
}
else if(indexPath.row >= [s.internalUserStorey count]){
//here are the cells for SubItem 2
}
return cell;
So thanks to all !!
Best Konstantin

You can take one NSMuatableArray and add references on that i.e.
NSMuatableArray *arr=[NSMuatableArray alloc]init];
//section no 1
[arr addObject:#"2"];
//section no 2
[arr addObject:#"1"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch(arr objectAtIndex[indexPath.row])
{
}
}
you can use this way by using references you want set cells.

Related

How to hide a particular row button in Dynamic UITableViewCell in Objective C?

I have implemented a Dynamic TableView, Textfield and Buttons. My problem is, when I hide the button in the first row of my UITableViewCell, the other five rows of cell button also get hidden.
Can any one suggest a solution for this issue?
I have tried below code..
ladiesdetails=[[NSMutableArray alloc]initWithObjects:#"2",#"0",#"0",#"0",#"0",#"0", nil];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[passengerdetailcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([[ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.malebutton.hidden=yes;
}
return cell;
}
Just put the else condition and make the button visible in cellForRowAtIndexPath method. If you have any other condition to show add that as well.
if([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2) {
cell.malebutton.hidden = YES;
} else {
cell.malebutton.hidden = NO;
}
like this
bool flag = ([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2)
cell.malebutton.hidden = flag
It happens because of cell reuse , you use allocate the cell like below one .
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PassengerDetailCell" forIndexPath:indexPath];
if([[self.ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.maleButton.hidden = TRUE;
}
return cell;
}

Default text in a Section if numberOfRowsInSection returns count 0

I have two sections in a TableView, having their respective sectionHeaders. numberOfRowsInSection is counted dynamically & it might also come out to be 0. So i want to display a default text somewhere in the section in case of 0 rows.
How do i do this ? (Working on iOS 6, XCode-4.2)
Why don't you display the default text in a cell in the "empty section"?
Instead of returning 0 rows return 1 and place the default text in there. It can be something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Verify is the section should be empty or not
if(emptySection == NO) {
return numberOfRowsInSection;
}
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(emptySection && indexPath.row == 0) {
cell.textLabel.text = #"This is the default text";
}
else {
// Display the normal data
}
return cell;
}
Update
The following code will avoid any actions when tapping an a cell that contains the default text.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(emptySection) {
return;
}
// Perform desired action here
}
Another solution is to completely prevent the cell from being selected:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)path
{
if(emptySection) {
retur nil;
}
return path;
}

How to setup cells in a static table divided in sections programmatically

I'm basically making a settings view on my app and I'm using a static table for it. So I divided the table into 3 sections each with one cell. Not programmatically I can easily label each cell and it works but programmatically I'm not able to initialize each cell. I can only initialize the first cell that gets repeated across the 3 sections. I would like a way to initialize a cell for each section but I can't find a method or a way to do that. My tableview also has reuseIdentifiers but it doesn't seem like the UITableViewController recognizes it.
This is what I have done so far.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EditProfile";
//UITableViewCell *cell = nil; //[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// More initializations if needed.
}
//Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
cell.textLabel.text = #"Hey";
//return cell;
return cell;
}
But what I want is the cell in the first section to be labeled: Edit Profile, the second one: Invite and the third:Logout
You have to make a condition for how to handle each different section within the table view.
if (indexPath.section == 0) {
cell.textLabel.text = #"Edit Profile";
}else if (indexPath.section == 1) {
cell.textLabel.text = #"Invite";
}else if (indexPath.section == 2) {
cell.textLabel.text = #"Log out";
}......
Check out the tableView delegate methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
(IF you're wanting to label the section headers and not the cells in the sections themselves)

Trouble Shoot: Programmatically add accessoryview to UITableViewCell messed up

I've stucked to this problem for couple days, and I've debugged it many times and can't figure out what's going wrong.
I have a UITableView that has three sections. I'd like to add buttons as accessoryView and UILabel as detailText to the second section.
It looked OK when View first loaded.
But after scroll it, it becomes
After couple scrolls, it freezes (not crash).
Below is my code, any advise will be appreciated.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return self.status.count;
}
else if (section==1)
return self.overrideCtrl.count;
else
return self.levelStatus.count;
// Return the number of rows in the section.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"section %#",indexPath);
if (indexPath.section == 0) {
cell.textLabel.text = [self.status objectAtIndex:indexPath.row];
NSLog(#"Status %#",cell.textLabel.text);
}
else if (indexPath.section == 1) {
cell.textLabel.text = [self.overrideCtrl objectAtIndex:indexPath.row];
cell.detailTextLabel.text =[NSString stringWithFormat:#"00:%02d",[[self.timeLeftArray objectAtIndex:indexPath.row] intValue]];//[[self.timeLeftArray objectAtIndex:indexPath.row] stringValue];
cell.accessoryView=[self.checkButtonArray objectAtIndex:indexPath.row];
NSLog(#"override %#",cell.textLabel.text);
}else{
cell.textLabel.text = [self.levelStatus objectAtIndex:indexPath.row];
NSLog(#"level %#",cell.textLabel.text);
}
return cell;
}
Thanks!
This problem is covered a lot on SO, it's because of cell reuse. You need to set the accessoryView to UITableViewCellAccessoryNone for your other sections, otherwise, a cell that was used for section one might be used in another section, and still have its accessory view.
You only have 1 CellIdentifier. You should have a different one for each section.

iOS - how to increase height of first row of UITableView only if Table is empty?

I am new at iOS development.
I want to check if my table is empty. If it is, I want to :
Increase height of the first row and display "No new messages!"
OR
Get rid of the table and just display "No new messages" in the center of the page.
How would I do that?
(Let's assume you have an array from which you want to populate the table view. This is a pretty standard way for populating table views. Let's call this theoretical array dataArr.)
In your data source:
- (NSUInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSUInteger)section
{
if ([dataArr count] == 0) {
// empty table
return 1;
}
return [dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"someCellID"];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"someCellID"] autorelease];
if ([dataArr count] == 0) {
// empty table
cell.textLabel.text = #"No new messages";
} else {
// configure as normally
}
return cell;
}
In your delegate:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)ip
{
if ([dataArr count] == 0) {
// empty table
return 88.0f; // 2 times the normal height
}
// else return the normal height:
return 44.0f;
}
I suppose you have an array of messages you are attempting to display
You can define custom height for cells with the function
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0)
return 80;
else
return 44;
}
and then, when you are creating the cells: (make sure the "no new messages" cell has a different cell identifier than the regular cells, so that your cell re-use won't mess things up)
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0) {
// create the "No New Messages" cell
}
else {
// create regular cells
}
}
This should help:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView numberOfRowsInSection:1] == 0) {
return 200;//some other height
}else{
return 44;
}
}
Basically you want to check against your data source to see if you have no messages in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([data count] == 0) return 100;
else return 44;
}

Resources