UITableViewCell - Custom cell margins - ios

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

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

cell.textlabel.text not working properly

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifire";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *makingString_fromArray = [appDel.Rhymes_Array objectAtIndex:indexPath.row];
NSArray *datatwoParts_Array = [makingString_fromArray componentsSeparatedByString:#"#"];
cell.textLabel.text = [datatwoParts_Array objectAtIndex:0];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
// Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
In my tableview, only first cell data show, others empty cell.i also NSLog the the array , data is there on each index but not show in the cell.
Only first cell data show.Thanks for help
Make sure you returning the count as,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [datatwoParts_Array count];
}
Also change the following line of code
cell.textLabel.text = [datatwoParts_Array objectAtIndex:0];
as.
cell.textLabel.text = [datatwoParts_Array objectAtIndex:indexPath.row];
EDIT:
Your code is little bit confusion, What you are trying to achieve here?
NSString *makingString_fromArray = [appDel.Rhymes_Array objectAtIndex:indexPath.row];
NSArray *datatwoParts_Array = [makingString_fromArray componentsSeparatedByString:#"#"];
Also please share your tableView:numberOfRowsInSection: method
check your
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([datatwoParts_Array count]!=0)
return [datatwoParts_Array count];
return 0;
}
Thanks all of You.
problem is that i am getting \r\n in Stringpath, thats why text other than first cell not showing.
i used NSCharacterset to remove unused characters from string.
Same problem i found in my project and this table method i founded try to this
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
cell.layer.backgroundColor = (__bridge CGColorRef)([UIColor clearColor]);//optional
cell.backgroundView = nil;
}
This should work with the 6.1 beta.
cell?.textLabel?.text = [datatwoParts_Array objectAtIndex:0]
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [datatwoParts_Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.text = [datatwoParts_Array objectAtIndex:indexPath.row];
NSLog(#" check the array value ======== %#",[datatwoParts_Array objectAtIndex:indexPath.row]);
}

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

Re-drawing static cells and giving them a new look IOS

i want to costumize the static cells in my storyboard table by drawing them with draw-rect, but how do i loop over all the cells and draw the rect?
I have read this tutorial but it only covers prototype cells:
http://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients
Assuming i have a draw rect method, how do i loop the static cells and apply it ?
EDIT 1:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// START NEW
if (![cell.backgroundView isKindOfClass:[CostumCellBackground class]]) {
cell.backgroundView = [[CostumCellBackground alloc] init];
}
if (![cell.selectedBackgroundView isKindOfClass:[CostumCellBackground class]]) {
cell.selectedBackgroundView = [[CostumCellBackground alloc] init];
}
// END NEW
cell.textLabel.backgroundColor = [UIColor clearColor]; // NEW
return cell;
}
If i use this in my Table View Controller, this is from the turtorial, i removed the parts that assigned value to the cell, since they are static.
This does not work becouse UITableViewCell has to return a value, and for some reason it does not.
I did assign the ID "Cell" to the rows, as he does in the tutorial
Try creating a custom table cell and doing this:
-(void)awakeFromNib
{
self.backgroundView = [[CostumCellBackground alloc] init];
self.selectedBackgroundView = [[CostumCellBackground alloc] init];
}
Then in your tableview controller, since your cells are static, you can assign them as IBOutlets and do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *returnCell;
switch(indexPath.row)
{
case 0: returnCell = self.staticCell0;
break;
case 1: returnCell = self.staticCell1;
break;
//etc
}
return returnCell;
}

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