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;
}
Related
How to create the following UITableView in a VC with 2 Columns? As UITableView only has one column. Is it possible to do it in StoryBoard? How to add shadow, round the corners and add the blurish borders as in the pic? All help or pointer is greatly appreciated.
Edited
How to set the 2 columns in Objective C? I can't find any help over the net especially for objective C. I think both the columns are UILabel.
As for the rows, do I need to set the Prototype Cell to 3 at Story Board?
The 1st column will have static values: NAME, DATE OF BIRTH, LOCATION. The 2nd column will fetch data from an array [John Doe, 06.03.1991, New York]
Do I create a loop to insert the data?
ANSWER
I followed this link and managed to create 2 custom UILabel columns and insert the columns from NSMutableArray
I can't get the following suggested answer to work, the answer suggested that we create an Empty User Interface and then add a Table View Cell and associate it back to the VC. I can't get it to work.
Mine was just creating the 2 labels at the prototype cell at the TableView and then following the link above.
create a customcell and put 7 UILabel first 4 label gives name basic setting,NAME,DOB,LOCATION than remain 3 label set blank and give tham property in cell.h file
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourdataarray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"yourcustomcellname"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"yourcustomcellname" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
id object = [yourdataarray objectAtIndex:indexPath.row];
if ([object isKindOfClass:[NSMutableArray class]]) {
NSMutableArray *Array = (NSMutableArray *)object;
//your cell property use here for fatch like below
[cell.NAME setText:[NSString stringWithFormat:#"%#",[Array objectAtIndex:i]]];//here i=which index your array contain name data
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return yourcustomcellheight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
refer this tutorial for customcell
https://www.appcoda.com/customize-table-view-cells-for-uitableview/
You need to use custom UITableViewCell, it seems the first column is just a label and the second column is a textfield.
In order to add boundary and add corner
myTableViewCell.contentView.layer.borderColor = [[UIColor grayColor] CGColor];
myTableViewCell.contentView.layer.borderWidth = 1;
myTableViewCell.contentView.layer.cornerRadius = 5.0;
myTableViewCell.contentView.clipsToBounds = YES;
myTableViewCell.contentView.layer.masksToBounds = YES;
check following example as a reference to learn how to create Custom UITableViewCell and use.
https://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
These columns you need to design in UITableViewCell. It can be anywhere of your preference, storyboard or xib.
For storyboard your view will be like this.
you need to add tags for all the components you add into the cell
- (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];
}
...
...
UILabel *label1 = (UILabel *)[cell viewWithTag:100];
label1.text = #"NAME";
UILabel *label2 = (UILabel *)[cell viewWithTag:200];
label2.text = #"John";
...
...
return cell;
}
The same way if you create xib your custom cell should be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
StackTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = (stackTableviewCell*)[[[NSBundle mainBundle] loadNibNamed:#"StackTableviewCell" owner:self options:nil] objectAtIndex:0];
}
...
...
label1.text = #"NAME";
label2.text = #"John";
...
...
}
For cell UI
self.lable1.backgroundColor = [UIColor lightGrayColor];
self.lable1.layer.borderColor = [UIColor colorWithRed:169/255.0 green:169/255.0 blue:169/255.0 alpha:0.8].CGColor;
self.lable1.layer.borderWidth = 1;
self.lable1.layer.cornerRadius = 5;
self.lable1.clipsToBounds = YES;
self.lable1.layer.masksToBounds = YES;
My problem is little complicated to define for me, even I understand what I am trying to do, I try my best to write this problem to understandable, guide me if I lack anything in question description.
So the problem is that I have a UITableView inside a UITableViewCell, both of UITableViews have custom UITableViewCell classes. Datasource and delegate of both outer an dinner UITableView's are in same class. I am handling the data array like this
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView==self.mainTableView) {
return titleArray.count;
}
return timeArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.mainTableView)
{
MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:#"cell"];
cell.label.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.view.backgroundColor = [UIColor greenColor];
return cell;
}
My custom cell's description
Main UITableViewCell cusotm class properties - UILabel (eg.
myLabel),UITableView(inside)
Inside UITableViewCell custom class properties - UIView (eg.
myView)
Now I want to show myView only on that cell when 'myLabel' matches with my array of string object. for eg.
if([myLabel.text isEqualTostring:#"22"]){
cell.myView.backgroundColor = [UIColor blackColor];
}
For eg. if I have 5 views and 2 label, now 2 rows in Main UITableView and 5 rows in inside UITableView but every view should be in the outer cell
only if it matches the myLabel'text.
P.S Don't think about where matching data coming from its not an issue issue of matching data.
As a final conclusion I want to create different row count for each inner UITableView cell.
Did you tried this?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if( tableView == self.mainTableView )
{
MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:#"cell"];
cell.label.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
MainTableViewCell *parentCell = (MainTableViewCell*)cell.superView;
NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];
NSString *text = [dataArray objectAtIndex:parentIndexPath.row];
if( [text isEqualTostring:#"22"] //your condition goes here
{
cell.view.backgroundColor = [UIColor blackColor];
}
else
{
cell.view.backgroundColor = [UIColor greenColor];
}
return cell;
}
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.
At the outset I would like to tell that I have researched and tried to follow stackoverflow links such as UISearchDisplayController and custom cell but still the problem persists
I have Search Bar and Search Display controller integrated into my UITableView. The search functionality works fine but the search results cell have the default white cell design and not my custom cell design which is used for the UITableView. Below is my code to make the Search Result's Cell design adapt my custom design.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchDisplayController.searchResultsTableView registerClass:[ItemCellTableViewCell class] forCellReuseIdentifier:#"Cell"];
if(!self.document){
[self initDocument];
}
self.filteredItemCatalogArray = [[NSMutableArray alloc]init];
self.itemCatalogTableView.dataSource = self;
self.itemCatalogTableView.delegate = self;
[self.itemCatalogTableView reloadData];
self.itemCatalogTableView.backgroundColor = [UIColor clearColor];
self.itemCatalogTableView.opaque = NO;
self.itemCatalogTableView.bounces = YES;
self.itemCatalogTableView.backgroundView = nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
if(tableView == self.searchDisplayController.searchResultsTableView)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell fields customization
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
return cell;
}
else
{
ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell fields customization
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
return cell;
}
}
What am I missing here ?
EDITED :
In the if block for search results I changed tableview to self.tableview and it gets the correct custom cell. But it takes the incorrect height which is smaller and so overlaps the rows for search results
In the if block for search results I changed tableview to self.tableview and it get the correct custom cell. But it takes the incorrect height which is smaller and so overlaps the rows for search results
to rectify the height issue I added the following line in viewdidload
self.searchDisplayController.searchResultsTableView.rowHeight =
self.tableView.rowHeight ;
1) If your cell are using a xib file you should add to viewDidLoad (or other method that will be called before tableView delegate)
[yourTableView registerNib:[UINib nibWithtName:#"your_nibName" bunde:yourBunde] forCellReuseIdentifier:#"yourIdentifier"]
also you should use registred identifier for custom cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CustomCellIdentifier = #"yourIdentifier";
if(tableView == self.searchDisplayController.searchResultsTableView)
{
// UITableViewCell customization
return cell;
}
else
{
ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[ItemCellTableViewCell alloc] initWithStyle:yourPreferedStyle reuseIdentifier:CustomCellIdentifier] // or other custom initialization
//put cell fields customization here
return cell;
}
}
If your custom cell is designed using a NIB try this
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"YourCellNibName" bundle:Nil] forCellWithReuseIdentifier:#"Cell"];
You have to get a reference of your tableView and then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
MyCell *cell = (MyCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
...
}
I'm trying to pass values to a table cell but when I replace the string with userdata.title
it shows up blank. userdata is an object of another class UserData, which is external and I imported it. UserData is accessed from all views to pass objects around from one controller to another. Now, how do I pass value to a table, and how do I keep it if I decide to add more values through my textfield?
- (void)viewDidLoad
{
tabledata = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
[super viewDidLoad];
//if I use "userdata.title" in replacemnent of #"hello" above, the table show up blank
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//--------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
//static NSString *CellIdentifier = #"Cell";
/* if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}*/
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
To pass your data from one ViewController (vc1 for example) to another (vc2), you can set a property it the destination controller. So for the UIViewController above, you should add in the .h
#property (nonatomic, retain) UserData *tableDatas;
Then, in the view controller vc1, you will have:
vc2.tableDatas = userdatas;
Finally, in the UITableViewDelegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you can just use:
cell.textLabel.text = [self.tableDatas objectAtIndex:indexPath.row];
There should be some adjustment but the main parts are here.
Good luck.