iOS- Error in displaying the array values in a table - ios

In my app, I have created a mutable array named "array".
array=[[NSMutableArray alloc]initWithObjects:#"a",#"b",#"c",#"d",#"e", nil];
My tableView contains only one row for multiple section, Each section has a customView which contains only one label.
My custom View name is ContentOfCell
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[cell.contentView addSubview:contentOfCell];
I have added the array to the label
contentOfCell.label.text=[array objectAtIndex:indexPath.section];
My problem is that it recognises only the first 4 values in the array and the first 4 values is getting repeated in the sections
I thing something is going wrong here
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row]+50;
}
If I change 50 by 100, the error occurs if not the values are inserted correctly
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:#"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[cell.contentView addSubview:contentOfCell];
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
cell.contentView.backgroundColor=[UIColor whiteColor];
}}
Here the view contains only 3 rows, when I scroll down the array is initiated again, then the values are printed again

I guess you are not reusing the cell correctly.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:#"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
ContentOfCell *contentOfCell;
//Initialization Part. declare objects here
if(cell== nil)
{
//Creation part. Create your new cell here. Do all UI actions here
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
contentOfCell.tag =100;
[cell.contentView addSubview:contentOfCell];
cell.contentView.backgroundColor=[UIColor whiteColor];
}
else{
// Reusable part. Reuse the UI controls here from existing cell
contentOfCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
}
//Assign all the data here
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
}
This is one of the simple mistake often committed by iOS Rookies.
Kindly check the Apple developer documentation here to understand how a tableview cell is reused
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

I implemented same code as below with grouped table.Everything is fine.Please compare your code with below code.
- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSMutableArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k", nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:#"da"];
if(aCell==Nil)
{
aCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"da"];
ContentOfCell *ContentOfCellObj=[[ContentOfCell alloc] initWithFrame:CGRectMake(0,0,320,50)];
ContentOfCellObj.tag=101;
[aCell.contentView addSubview:ContentOfCellObj];
}
ContentOfCell *ContentOfCellObj=(ContentOfCell*)[aCell.contentView viewWithTag:101];
ContentOfCellObj.nameField.text=[NSString stringWithFormat:#"%#",[arrData objectAtIndex:indexPath.section]];
return aCell;
}

Use this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier=#"CellIdentifier";//or your custom name
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
contentOfCell.tag = 101;
[cell.contentView addSubview:contentOfCell];
cell.contentView.backgroundColor=[UIColor whiteColor];
}
ContentOfCell * contentOfCell = (ContentOfCell*)[cell.contentView viewWithTag:101];
contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
return cell;
}

Make sure you set
myTableView.dataSource = self;
and these two delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}

Try this code, surely it will resolve your problem.
Step 1. First declare array as global variable
#interface YOUR-CLASS-NAME ()
{
NSMutableArray *array
}
Step 2. Now set no of sections you need. This means how many time do you want to repeat the array values in table continuously.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Step 3. Now set no of rows you need to display from your array. This means no of values from your array need to display
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}

Related

objective c UItableView make two columns

i am try to to making two columns in my tableview . i am spend huge time google ,to solve but i am failed, here i am share my code,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData =[NSArray arrayWithObjects:#"Hello",#"Hello2",#"Hello3",#"Hello",#"Hello2",#"Hello3", nil];
tableView=[[UITableView alloc]init];
tableView.frame = CGRectMake(0,50,320,600);
tableView.delegate = self;
tableView.dataSource = self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[tableView reloadData];
[self.view addSubview:tableView];
}
for table
/*Start tableView */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell.contentView viewWithTag:#"H"];
label.text = #"Demo";
label = (UILabel *)[cell.contentView viewWithTag:#"h2"];
label.text = #"Demo2";
return cell;
}
Thanks in advance .
You can find several demos for your requirement. I’ve specifically used this library and made some changes according to my requirement. You can do the same and hope it helps you like it did to me.. ;) Here’s a link :
 https://github.com/kingiol/XCMultiSortTableView
What about to create one column for the line. But eg.: have in that column two buttons that would invoke some action, you would not rely on:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
But on some
- (IBAction)doSomething
And if you will do some graphic separation, it will look like, you want it to look like

UITableViewCell - Custom cell margins

I have built a UITableView which contains custom cells which are fed from a dynamic array.
My array code is here -
feedData *feedData1 = [[feedData alloc] initWithFeedTitle:#"Fishing Article" FeedGroup:#"articleP" FeedImage:#"blah" FeedDate:#"1/11/13" FeedDesc:#"Check out this swimming article..." FeedDestination:#"articlesSub" ];
feedData *feedData2 = [[feedData alloc] initWithFeedTitle:#"Runnung Article" FeedGroup:#"articleP" FeedImage:#"blah" FeedDate:#"4/11/13" FeedDesc:#"Check out this marvellous article..." FeedDestination:#"articlesSub" ];
feedData *feedData3 = [[feedData alloc] initWithFeedTitle:#"Runnung Article" FeedGroup:#"articleP" FeedImage:#"" FeedDate:#"4/11/13" FeedDesc:#"Check out this marvellous article..." FeedDestination:#"articlesSub" ];
self.HpFeedArray = [NSArray arrayWithObjects:feedData1, feedData2, feedData3, nil];
I want a margin above each cell - the only way I could figure how to do this was to split the cells up into sections, apply a section header and show one row in each section - my current code is like this -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.HpFeedArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10.; // you can have your own choice, of course
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
I then applied the relevant data to the relevant cells as follows -
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"CellFeed";
feedData *f = [self.HpFeedArray objectAtIndex:indexPath.row];
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CellHp_RecArticleWithImage *CellArticle = (CellHp_RecArticleWithImage *)cell;
CellArticle.selectionStyle = UITableViewCellSelectionStyleNone;
CellArticle.artTitle.text = f.FeedTitle;
CellArticle.artImg.text = f.FeedDesc;
CellArticle.artDate.text = f.FeedDate;
CellArticle.textLabel.backgroundColor=[UIColor clearColor];
return CellArticle;
The above displays the cells spaced apart - but displays the same data 3 times over! Can anyone help me get my head around what im doing wrong!?
Cheers
Paul
The problem is in this line
feedData *f = [self.HpFeedArray objectAtIndex:indexPath.row];
since you splitted your tableView with a row for each section if you use row for the index you will get always 0 since there is only one row. so the solution is
feedData *f = [self.HpFeedArray objectAtIndex:indexPath.section];

iOS7 table index appearing as white bar

I am trying to set up a table index but it is showing up as a white bar on the right. I am using storyboard. Does anyone know what is going on? Thanks
Code:
#pragma mark - Tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections in a tableview
return self.arrayOfCharacters.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return the height of each cell
return 55;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// Return table index
return [NSArray arrayWithArray: [#"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#" componentsSeparatedByString:#"|"]];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
// Match the section titls with the sections
NSInteger count = 0;
// Loop through the array of characters
for (NSString *character in self.arrayOfCharacters) {
if ([character isEqualToString:title]) {
return count;
}
count ++;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the each section
return [[self.objectsForCharacters objectForKey:[self.arrayOfCharacters objectAtIndex:section]] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// Return the title for each section
return [NSString stringWithFormat:#"%#", [self.arrayOfCharacters objectAtIndex:section]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"preplanCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Create a pointer to our preplan
LBPrePlan *preplan = [[self.objectsForCharacters objectForKey:[self.arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = preplan.name;
cell.detailTextLabel.text = preplan.address;
return cell;
}
Found my problem. With iOS7 you have the ability to set sectionIndexBackgroundColor. Turns out it was working the whole time.
You can change the tableview indexset background color in ios7 using the following code
[[UITableView appearance] setSectionIndexBackgroundColor:[UIColor clearColor]];
[[UITableView appearance] setSectionIndexTrackingBackgroundColor:[UIColor lightGrayColor]];
[[UITableView appearance] setSectionIndexColor:[UIColor darkGrayColor]];

TableView Cell contents not shifting in editing mode iOS 6

I have subclass of UITableViewCell. Here is my setup
I have used Autolayout. Now when user swipe on cell , I can see "Delete" button , but it overlaps my right side imageview. Cell contents are not shifting. Here is my code
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recentItemsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProductCell";
ProductCell *cell = (ProductCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:#"ProductCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
tableView.separatorColor = [UIColor clearColor];
}
Product *product = [self.recentItemsArray objectAtIndex:indexPath.row];
cell.lblProductName.text = product.name;
cell.lblNoOfBids.text = [NSString stringWithFormat:#"%#",product.numberOfBids];
cell.lblBuyItNowPrice.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.buyItNowPrice];
cell.lblTimeRemaining.text = product.timeRemaining;
cell.lblLatestBid.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.currentBidPrice];
cell.lblUserName.text = product.sellerName;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110.0f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[CommonUtils showAlertWithTitle:#"Alert" andMessage:#"Do you want to delete this product?"];
}
}
I saw several question on Stackoverflow , but didn't get any solution.
Can any body tell me what I am doing wrong ? Any kind of help is highly appreciated. Thanks
Update::
I tried adding UIImageView programatically. still image views are not shifting
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260, 0, 50, 60)];
imageView.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:imageView];
UIImageView* imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(20.0, 0, 60, 60)];
imageView1.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:imageView1];
}
//cell.textLabel.text = #"ABC";
// Configure the cell...
return cell;
}
It happend with me. Verify if some cell property have the same name from other property in the view where the UITableView is.

IOS - UISegmented in UITableview

I insert a segmented in uitableview like this url image. If you see the url image, now i'm working ok everything data in uitable,
and...
When i scroll down table i wanna part 1: Image(in url) will scroll scroll to part 2: segmented will stop and if you still scroll uitable part 3: the data will be scroll (keep segmented on the top).
Have you any idea it. Thanks
I just test it, it is working!
Set your UITableView pain
UITableView has two section and each section has only one row.
When edit your section header, make sure only second section has header - second header will be your part2.
first section first row is your part1.
second section first row is your part3.
That's it : )
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
#try {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
switch (section) {
case 0:
{
//nothing
}
break;
case 1:
{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
lbl.text = #"second";
lbl.textColor = [UIColor blackColor];
[containerView addSubview:lbl];
}
break;
}
return containerView;
}
#catch (NSException *exception) {
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 1000.0;
}

Resources