hi i have 2 table and i use pod ASOXScrollTableViewCell
when in my table. I know where the information came from the square.
if (tableView == myTablePromotion) {
return 2;
}else if (tableView == myTableBestSeller) {
return 1;
}
and but when in delegate method horizontalScrollContentsView cellForItemAtContentIndexPath i don't know for check it's not Table
What do I have to do.
Related
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.
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
}
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)
I'm starting off developing an iPhone app. I need to present a drop down box for the customer to pick a value which will essentially be sent downstream to a database upon submit. I'm clearly using a UIPickerView for the drop down list, built from IB.
Once my view loads, the Drop down list is enabled, with all values showing.
Question is:
Can I only expand the drop down list once it's clicked?
Can I retract the list once the user selects a value?
I'm thinking very web-centric in terms of drop-downs, but I could have this all wrong.
Your view controller needs to implement the UIPickerViewDataSource as well as the UIPickerViewDelegate. In the XIB of the UIPickerView you need to link the File's Owner with the UIPickerView's delegate and datasource.
Like in the UITabelDataSource you need to provide the number of components (table: sections) inside [numberOfComponentsInPickerView]. Then you need to provide the number of rows for each component in [numberOfRowsInComponent]. Finally you need to provide the title for the each entry inside [titleForRow].
Now to bring everything to life you can use [didSelectRow] to load the next component if the previous one was selected.
Attention: UIPickerView has a little bug not to call [didSelectRow] when the component is filled / changed. In order to make it work more smoothly I added a "None" entry as the first entry which is a non-selection and does not cause the next component to be loaded.
This is a rudimentary code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3; // Number of Components aka Columns
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if( component == 0 ) {
return [self.firstColumnEntries count] + 1;
} else if( component == 1 ) {
return [self.secondColumnEntries count] + 1;
} else if( component == 2 ) {
return [self.thirdColumnEntries count] + 1;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog( #"titleForRow(), component: %d, row: %d", component, row );
NSDictionary *item = nil;
if( row == 0 ) {
return #" --- ";
} else {
int correctedRow = row - 1;
if( component == 0 ) {
item = [self.firstColumnEntries objectAtIndex:correctedRow];
} else if( component == 1 ) {
item = [self.secondColumnEntries objectAtIndex:correctedRow];
} else if( component == 2 ) {
item = [self.thirdColumnEntries objectAtIndex:correctedRow];
}
return [item objectForKey:#"name"]; // My objects are NSDictionarys
}
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
DLog( #"SSVC.didSelectRow(), component: %d, row: %d", component, row );
ActionRequest *action = nil;
if( row == 0 ) {
if( component == 0 ) {
[self refreshFirstColumn:self.firstColumnEntries];
} else if( component == 1 ) {
...
}
} else {
// Last Column is selected. Now selection is complete
...
}
}
Because the UIPickerView takes away a lot of space and you cannot change it size I would recommend to place it on an additional UIView (make the rest transparent) and display it when the user selected the field that is assigned to the value (UITextField). When the selection is done (either when last value is selected or when the user enter a button) you let the View disappear and set the value onto the UITextField. Make sure that the UITextField cannot be edited and that entering it make the View with the Picker View appear. When done make sure that you also move to the next field.
You have a couple options:
Probably easiest for a beginner: push a new view (either as a modal view or onto the nav stack) that presents the list either as a table view where they click the item they want or as a pickerView where it scrolls.
Review the twitter app 'my profile' view. While I haven't done this personally, I think it's simply using the didSelectRowAtIndex to determine which section the user clicked and then filling an array of values for that section before calling [tableView reloadData]
Hi i m using 2 tables in a single view and want to get the indexpath of the tables. I use
the below code to get the indexpath. but stil categoryId getting effected whatever table i access not the other thing. Please help...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView=categoryTblView)
{
categoryId=indexPath.row;
NSLog(#"categoryId=%d",categoryId);
}
else if(tableView=regionTblView)
{
regionId=indexPath.row;
NSLog(#"regionId=%d",regionId);
}
}
To test equality here, you need to use == instead of a single equal sign (which is for assignment).
So the code should be:
if (tableView == categoryTblView)
{
...
}
else if (tableView == regionTblView)
{
...
}