objective c UItableView make two columns - ios

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

Related

Create a table on objective-c code without storyboard

I can't create table on objective-c, cause I start learning objective-c.
Google can't help me, I tried.
I hope what u can help me
//Update: Below Nick showed the correct answer. After some time, I was prompted to answer this question and now I can share with you the answer. I want to be useful for such guys as I was
- (void)viewDidLoad
{
[super viewDidLoad];
//Here we initialize the table, create delegates and display it on the screen
arrData=[[NSArray alloc]initWithObjects:#"ONE",#"TWO",,#"THREE", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Cells initialization
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
return cell;
}
For default tableview cell with array:
- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSArray alloc]initWithObjects:#"ONE",#"TWO",,#"THREE", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
return cell;
}
IF YOU ARE USING XIB
You have to create tablviewcell file and design your own cell.
Import your cell file too..
#import "selectedcell.h"
- (void)viewDidLoad {
[super viewDidLoad];
array =[[NSArray alloc]initWithObjects:#"AAAA",#"BBBB",,#"CCCC", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.tablview registerNib:[UINib nibWithNibName:#"SelectedTableViewCell"
bundle:nil] forCellReuseIdentifier:#"cellidentifier"];
self.tablview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SelectedTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:#"cellidentifier" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.label.text = [Nsstring stringwithformate:#"%#",[array objectatindex:indexpath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//After selecting row
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

Text not being displayed in cells

Trying to get my app to display text in its cells. Been scratching my head for a while. This is the code used to display the text but nothing appears. Any advice?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
NSArray *titles = #[#"Quick Glance", #"Your Home", #"Invites"];
cell.textLabel.text = titles[indexPath.row];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}
If you have not add the following code in ViewDidLoad() method, please do so.
self.tableView.dataSource = self;
self.tableView.delegate = self;
Firstly you should break this out differently so that you are not re-creating your backing store every time you are producing a cell.
More code would be required in order to ascertain exactly what the issue but at minimum you could put the following in to verify this.
#implementation ViewController {
NSArray *_titles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_titles = #[#"Quick Glance", #"Your Home", #"Invites"];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = titles[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
This is the bare minimum you will need above in order to show some information in the tableview
Now the question is are you using a UITableViewController where it already has set the dataSource and delegate methods or are you using a UIViewController where the dataSource and delegate have not been set (either in code or through XIB or Storyboard)?
If you do not have the delegates set up you need to set them up either through the storyboard or through the code, and if through code and using UIViewController make sure you have a reference to the UITableView otherwise you will not be able to set the datasource.

iOS- Error in displaying the array values in a table

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];
}

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: uitableview redraw objects on first cell to bottom cells

i have a uitableview with custom cells.. with normal code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DDMainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[DDMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
the problem is when i select one cell i add progress bar on the cell that download data online.. but when i Scroll down i find that every 10 cells have the same progress bar .. how can i prevent this behavior ?
Try this,
- (void)viewDidLoad
{
[super viewDidLoad];
dataarr=[[NSMutableArray alloc]init];
indexarr=[[NSMutableArray alloc]init];
mytableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
mytableview.dataSource=self;
mytableview.delegate=self;
[self.view addSubview:mytableview];
for (int i=0; i<30; i++) {
[dataarr addObject:[NSString stringWithFormat:#"%d",i]];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataarr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text=[dataarr objectAtIndex:indexPath.row];
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[act setFrame:CGRectMake(50, 20, 20, 20)];
act.hidden=YES;
[cell.contentView addSubview:act];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[act startAnimating];
act.hidden=NO;
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[mytableview reloadData];
return;
}
[indexarr addObject:[dataarr objectAtIndex:indexPath.row]];
[mytableview reloadData];
}
Make sure, when downloading is complete, then remove this object from indexarr....
That's because your cells are getting reused; UITableView will put off-screen cells into the reusable cell queue, and dequeue them for reuse if the reuseIdentifier matches. You should use some other data structure (e.g. NSArray or NSDictionary) to track which indices/cells have already been tapped. Then, in the method you showed above, regardless of whether the cell was init-ed or dequeued, set the progress bar according to your underlying data structure.
Here your used UITableViewCellIdentifier is reuseIdentifier. Which will work for all Cells are same type. Now your are taking once cell with progress bar. Now it will different from all cells data.
So use one more tableview cell for progress bar, or while reloading table remove the Progress bar which is exists already and add again. for this use tag for progress bar.

Resources