Collapse UITableViewCell on second click - iphone

I want to expand a UITableViewCell with this function, calling from didSelectRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedCellIndexPath != nil &&
[selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 128;
return 43;
}
Clicking on a cell expands it, clicking on another cell collapses to old and expands the new cell. So far so good. But I also want to collapse the cell, when clicking on an expanded cell, and don't know how?
As a bonus: the content of a not-expanded-cell is shown, overlapping the cell below. How can I prevent showing content that should be cropped by the size of a collapsed-cell?

Sounds like you just need to check in didSelectRowAtIndexPath:…, if indexPath is equal to selectedCellIndexPath, then set selectedCellIndexPath to nil.
You didn't provide that code, so I'm just guessing.

You can also use a BOOL declared in your header and then int your
-didSelectRowAtIndexPath:indexPath {
if(your condition) {
BOOL Selected = YES
[tableView reloadRowAtIndexPath:indexPath]
}
}
and in
-heightForRowAtIndexPath:indexPath {
if(indexPath.Section == TheOneYouWant && indexPath.row == TheOneYouWant && Selected)
return YourHeight;
else if (indexPath.Section == TheOneYouWant && indexPath.row == TheOneYouWant && !Selected)
return yourOtherHeight;
}

Related

Load cell selectively in TableView

I need to return cell in tableView:cellForRowAtIndexPath: only when some condition is true,for example:
if (condition == true)
return nil;
else
return cell;
Returning nil gives me an error.
You'll need to do a little more to conditionally have a cell in a UITableView.
Let's assume you have 3 cells, and the first one is conditional. The first thing you need to do make your table have either 2 or 3 cells based on this condition:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(condition) {
return 3;
} else {
return 2;
}
}
Next you can return the actual cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(condition) {
if(indexPath.row == 0) {
//create and return conditional cell
} else {
//create and return normal cell
}
} else {
//create and return normal cell
}
}
And voila!
PS: (This is assuming everything is done in a single section. If you need to break that out into multiple sections we can get into that as well).
This is because you cannot have blank spaces in your UITableView, that is not allowed, you must at least return an empty cell. What are you trying to do?
The error presents when the TableView tries to retrieve the next cell and gets nil, it has to get the next cell no matter what.
Depending on exactly what you are intending to do here could you not do perform the conditional test before making a call to tableview:cellForRowAtIndexPath:
EG
if( someCondition )
{
[self.tableview cellForRowAtIndexPath:indexPath]
}
Or if your out come is to only display the table cells that meet a certain condition the I suggest you create a function that would copy those elements from your tableview data into an NSArray that you would use to display the desired/conditional table data.
IE
-(void)composeVisibleTableData
{
[m_visibleTableData removeAllObjects];
for( NSObject* dataObject in m_tableDataArray )
{
if( someCondition )//dataObject meetsCondition
{
m_visibleTableData addObject:dataObject];
}
}
}
Then in your UITableDelegate functions for numberOfRowsInSection: and tableview:cellForRowAtIndexPath: reference m_visibleTableData as the UITableView Data Source.
You should check condition when Cell's datasource is set :)
and Filter your data with condition.

Possible to tag a section in UITableView?

I have a uitableview with cells whose behavior depends on what:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 2) {
do x;
}else{
do y;
}
}
It kind of works when you are dealing with a single table, but when you try to add sections, or subclass things, this type of "magic numbers" break very easily.
My question is, is it possible to "tag" the section? so instead of doing section==1, we will do:
indexPath.section.tag=="user_stats" {load x}
indexPath.section.tag=="answers" {show answers}
indexPath.section.tag=="page" {show pagination}
Yeah, just set up an enum somewhere.
enum { kSectionUserStats = 0,
kSectionAnswers,
kSectionPage };
Then:
if(indexPath.section == kSectionPage)
{
// do x
} else if(indexPath.section == kSectionAnswers)
{
// do y
}
// etc.
This also lets you reorder your sections really easily just by changing their ordering in the enum.
If you are displaying section headers with titles, then the titles themselves can become your tags.
if([self titleForHeaderInSection:indexPath.section] == #"MySection1")
{
//do something
}
else
{
//do something else
}

Getting a cell number from cellForRowAtIndexPath

I'm making an iPhone app with a Table View, and I'm trying to place a different icon / image next to each cell on a table.
I know that you set the image in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath with code that looks like this:
UIImage *image = [UIImage imageNamed:#"image.png"];
cell.imageView.image = image;
What I'm trying to figure out is, how do I target a specific cell, so that it has a unique image? Like:
if (cell.number == 0) {
//Use a specific image
}
else if (cell.number == 1) {
//Use a different image
Thanks!
The indexPath variable contains information about the cell's position. Modifying your example:
if (indexPath.row == 0) {
// Use a specific image.
}
See the NSIndexPath Class Reference and NSIndexPath UIKit Additions Reference for more information. It's also important to note that cell numbers reset in each section.
Use the row (and possibly also section) properties in the NSIndexPath passed to your tableView:cellForRowAtIndexPath: method to identify which cell is being queried.
this function is passed an index path, which has a section and a row. indexPath.row will pass back an integer you can check.
When cellForRowAtIndexPath is executed you have access to the indexPath variable, so if you want to customize the cell style depending on the cell index you can do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// code for cell 0
}
else {
if (indexPath.row == 1) {
// code for cell 1
}
}
}
This is just an example, I don't think that customizing your cells by using if conditions is the best idea, but it shows you how to do what you need.
Remember that indexPath contains the section of the table too. If you are using a Grouped table view, you need to manager the section too. For example:
if (indexPath.section == 0) {
// section 0
if (indexPath.row == 0) {
// code for section 0 - cell 0
}
else {
if (indexPath.row == 1) {
// code for section 0 - cell 1
}
}
}
else {
if (indexPath.section == 1) {
// section 1
if (indexPath.row == 0) {
// code for section 1 - cell 0
}
else {
if (indexPath.row == 1) {
// code for section 1 - cell 1
}
}
}
}
For a slightly nicer looking approach I would put all the images you want to use into an array:
_iconArray = #[#"picture1.png", #"picture2.png", #"picture3.png"];
This means that when you come to the cellForRowAtIndex function you can say only:
cell.imageView.image = [UIImage imageNamed:_iconArray[indexPath.row]];
This is also easier if you have more than one section, this time you can make an array of arrays, each containing the required pictures for the different sections.
_sectionsArray = #[_iconArray1, _iconArray2, _iconArray3];
cell.imageView.image = [UIImage imageNamed:_sectionsArray[indexPath.section][indexPath.row];
This immediately makes it very easy to modify the pictures (as you are only dealing with the arrays. And much easier if you have more rows and sections (imagine doing it manually for 100 rows)

How to add header title for every Section in Table

I have four sections in my UITableView, how do I add a header for each section?
I am using the following code, but it's not working.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return #"Tasks";
if (section == 1)
return #"Appointments";
if (section == 2)
return #"Activities";
if (section == 3)
return #"Inactivities";
}
Are you sure that you've implemented tableView:heightForHeaderInSection:?
In your case the titles were actually set but the headers' heights were 0 by default. Add this UITableViewDelegate method if you haven't yet:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40; //play around with this value
}
P.S.: Consider using switch statement in such methods
If your section Header height is constant for each section then you do'nt need to implement heightForHeaderInSection delegate method, And you could use as below
myTableView.sectionHeaderHeight = 40 ;
In Apple Documentation
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Change your if statements like this
if(section == 0)
return #"ABC";
else if (section == 1)
return #"XYZ";
else
return #"PQR"
I hope this helps. Please let me know if I can be of any more help.
Pinku:)

indexPath.row is returning null in tableView didSelectRowAtIndexPath

indexPath.row returns null in tableView didSelectRowAtIndexPath:indexPath
I thought it was supposed to return the row selected.
When i look at indexPath in the debugger it (corectly) returns: "2 indexes [0, 0]"
Am I missing something?
Well null is 0. The row property is type int -- perhaps you're mistakenly using it as an object or pointer.
You mention that you want the actual "row" that was selected. If you mean you want the cell that was selected, here's how:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Note that if you somehow programmatically select a row that's not currently visible, this will return nil.
indexPath.section tells you what section it's asking for.
indexPath.row tells you which row in that section it wants.
Both start at 0.
You want to do something like this:
if (indexPath.section == 0) { // 1st section
if (indexPath.row == 0) return cell1;
if (indexPath.row == 1) return cell2;
if (indexPath.row == 2) return cell3;
} else if (indexPath.section == 1) { // 2nd section
if (indexPath.row == 0) return cell4;
if (indexPath.row == 1) return cell5;
if (indexPath.row == 2) return cell6;
}
return nil;
It returns the PATH to the row selected, not the row (or cell) itself.
indexPath(0,0) is the first cell of the first section. Depending on your test you are running, this may be the correct result.