I have an issue while displaying BannerAds for 1st and every 5th row. While displaying data, first row is replaced with banner ads and every 5th row data is replaced with banner ads...How to overcome this. Following is what i have tried.TIA
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger n;
n= [array count];
return n;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
//configure ad cell
for(UIView* view in cell.contentView.subviews) {
if([view isKindOfClass:[GADBannerView class]]) {
[view removeFromSuperview];
}
}
else
{
Title.text=[NSString stringWithFormat:#"%# ",[dict objectForKey:#"Name"]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 5 == 0)
return 60;
else
return 153;
}
You need to increase the number of cells you return in numberOfRowsInSection and account for the added rows in cellForRowAt
The number of advertisements will be 1 + n/5 (The first row and then every 5th row), so the number of cells in your table will be n + n/5 + 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger n;
n= [array count];
return n/5 + n + 1;
}
Now, some of the cells you need to return from cellForRowAt will be ads and you will need to account for this when accessing your data array. The index you need is the row number - the number of advertisement rows that have come before it. This is index/5 + 1 (The first row and every 5 rows).
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
AdCell *cell = (AdCell *)[tableView dequeueReusableCellWithIdentifier:"Ad" forIndexPath: indexPath];
...
NSLog(#"Showing an ad at row %ld",indexPath.row);
return cell;
else
{
NSInteger index = indexPath.row - indexPath.row/5 - 1;
NSDictionary *dict = myArray[index];
NormalCell *cell = (NormalCell *)[tableView dequeueReusableCellWithIdentifier:"Normal" forIndexPath: indexPath];
cell.title.text=[NSString stringWithFormat:#"%# ",dict["Name"]];
NSLog(#"Showing a normal row at row %ld (data from element %ld of array)",indexPath.row,index);
return cell;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a UITableView that contain 2 different custom cell. One is ExerciseTime and one is RestTime. I want my RestTime cell to be inserted between every 2 ExerciseTime cell :
Here is my code:
-Height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 2 == 1) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
-Number of row in section (i couldnt find a way to set number of row for both RestTime and ExerciseTime)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.preset.blocks.count * 2) - 1;
}
-Cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
//CustomCell
return restTimeCell;
}else{
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:indexPath.row];
//CustomCell
return exerciseTimecell;
}
return nil;
}
How can I create a tableView with 2 custom cell like this?
First return count as total of two array in numberOfRowsInSection and then in cellForRowAtIndexPath try some thing like this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 3 == 0) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (indexPath.row % 3 == 0) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
//Access you object from array like this
NSInteger index = (NSInteger) (indexPath.row / 3);
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:index];
}
else {
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
//Access you object from array like this
NSInteger index = (indexPath.row - (NSInteger) (indexPath.row / 3));
ExerciseTime *exerciseTime = [self.exerciseTimeArray objectAtIndex:(index - 1)];
}
return cell;
}
Output: I have try this by setting background color as Red color for RestTimeTableViewCell and Green color for ExerciseTimeTableViewCell.
if you have n exerciseTime, you will be able to insert n-1 restTimes. So
Try this...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.exerciseTime.count + (self.restTime.count >= self.exerciseTime.count) ? self.exerciseTime.count-1 : self.restTime.count;
}
Now, add exerciseTime in even position, and add restTime in odd position until current index exists in both of those lists. if current index exceeds restTime, then continue adding exerciseTime. Let me know if that works.
I want UITableView always has the text "No more items" in the last cell, I want this row appears regardless of number of items, include 0 items. How could I do?
You can get the total row of a section by using numberOfRowsInSection: method.
NSInteger total = [self.tableView numberOfRowsInSection:indexPath.section];
// Check if last cell or no cell.
if(indexPath.row == totalRow -1 || indexPath.row == 0)
{
// Do your operation here
}
You can perform this in your cellForRowAtIndexPath or willDisplayCell methods. Hope it helps.
Just add the row regardless of your other items:
- (void)buildMenu {
[_menuItems removeAllObjects];
// ... add your items or not if you have 0
YourCellItem *lastItem = [[YourCellItem alloc] initWithTitle:#"No more items"];
[_menuItems addObject:lastItem];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"yourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
YourTableViewCell *basicCell = (YourTableViewCell *)cell;
YourCellItem *item = [_menuItems objectAtIndex:indexPath.row];
basicCell.itemTitle.text = item.title;
return cell;
}
I have a view that has 2 independent cells. Everything is working properly, except the numberOfRowsInSection return that must be different for each cell.
I want my cell "dummy" to only return 1 or 2.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//I need to add some code in here that will say that
//if my cell = dummy then return 1 else
//Returning table count based on number of Team Names
return _teamNames.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
return indexPath.row + 20 - indexPath.row;
}
if (indexPath.section == 0 && indexPath.row == 18) {
return indexPath.row + 50 - indexPath.row;
} else {
return indexPath.row + 26 - indexPath.row;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *dummycell = #"dummy";
dummytest *cellar = [tableView dequeueReusableCellWithIdentifier:dummycell
forIndexPath:indexPath];
cellar.righthere.text = #"hello";
static NSString *CellIdentifier = #"StandingsIdent";
StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
Any ideas? Thank you.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// I need to add some code in here that will say that if my cell = dummy then return 1 else
// You need to set this bool value in your code somewhere - where you have some sort of toggle button between - Dummycell and StandingsViewCell
if(isDummyCell)
{
return 1;
}
else
{
return _teamNames.count; // Returning table count based on number of Team Names
}
}
You can use a NSMutableDictionary as source for your Table View data (i.e. "MyData"), so you can control the behaviour of every cell directly by the data source.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//list of elements
if (self.keys.count == 0) {
return 0;
}
NSString *key = [self.keys objectAtIndex:section];
NSArray *mySection = [MyData objectForKey:key];
return [mySection count];
}
where keys is the array that holds your sections. When you load your data, if you want an alphabetical order, you've only to do:
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.MyData allKeys] sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
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.
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;
}