I want to set two background colours for single row like in this image.
The following is my code which is for array and below that there are tableView delegate methods.
-(viewDidLoad)
if([managedObject valueForKey:#"personality_company_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Personality %#",[managedObject valueForKey:#"personality_company_master_values"]]];
}
if([managedObject valueForKey:#"video_tag"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Tag %#",[managedObject valueForKey:#"video_tag"]]];
}
if([managedObject valueForKey:#"industry_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Industry %#",[managedObject valueForKey:#"industry_master_values"]]];
}
}
Here is tableView delegate methods
- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = #[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
];
}
return _myTableViewCells;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self->_displayValues.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = self.myTableViewCells[indexPath.row];
NSManagedObject *managedObject = [self.devices lastObject];
UILabel *lbl=(UILabel*)[cell viewWithTag:900];
[lbl setText:[managedObject valueForKey:#"personality_company_master_values"]];
lbl.textColor=[UIColor blackColor];
[cell.contentView addSubview:lbl];
return cell;
}
Set background color for labels or try to add two view inside of your table cell content view
not enough repo to post as a comment
Add subviews, use layout constraints to size the subviews, apply the background colour to each of the views.
It's possible that you could use the labels in the cell to do that without using additional views, you'd just need to set the constraints appropriately. It actually looks like it might be a background colour on the cell and a background colour on just one of the labels...
You should have to take two Labels in subview of UITableViewCell and give two different colors to them.
Write this in cellForRowAtIndexPath method.
- (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] autorelease];
for(UIView *eachView in [cell subviews])
[eachView removeFromSuperview];
//Initialize Label
UILabel *lbl1 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl1 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl1 setTextColor:[UIColor grayColor]];
lbl1.text = YOUR CELL TEXT;
[cell addSubview:lbl1];
[lbl1 release];
UILabel *lbl2 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl2 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = YOUR CELL TEXT;
[cel2 addSubview:lbl2];
[lbl2 release];
return cell;
}
If you are not familiar with customisation of UITableViewCell means if you dont know how to add two different labels in the subview of UITableViewCell, then Follow this tutorial to customise your tableviewcell
Happy Coding!
plz take two label in uitableview cell and you can access them
UILabel *label =(UILabel *)[cell viewWithTag:yourtagnumber];
set this identifier value , and set the style to custom
the in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"youridentifier"];
NSManagedObject *managedObject = [self.devices lastObject];
UILabel *lbl=(UILabel*)[cell viewWithTag:900];
[lbl setText:[managedObject valueForKey:#"personality_company_master_values"]];
lbl.textColor=[UIColor blackColor];
return cell;
}
Related
I'm using below code to add a text label into a static table with 4 sections and 3 row in each.
for some reason, when I first run the app its fine, but when I scroll up and down, the UItable doesn't show up properly. Can someone tell why?
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"%d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
return cell;
I don't think you need and else statement while building your cell, this is an edited code that may work out for you:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
// Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%d",indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}
/*else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"cell nr. %d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
*/
return cell;
}
I've commented your last lines of code so you won't lose them. If you also set the style of your TabelView as "Grouped", the result you'll get is the following:
http://s2.postimg.org/820thcmy1/i_OS_Simulator_Screen_shot_26_giu_2014_14_46_41.png
Hope this helps!
In my my application I want to change the cell text color with out disappearing the cell separator.And I am using the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; // set color here
[cell setSelectedBackgroundView:selectedBackgroundView];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
But when I clicked the cell the cell separator also is disappearing ? How to change the text color without hiding the separator ?
It seems the cell separators are a problem for a lot of people. So, I would say rather than doing what I suggested to disable the selection, it would be easier to set the cell separator to 'none' and manage the separator yourself in the background and selected background views:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
[backgroundView setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:backgroundView];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, cell.frame.size.height - 1, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *backgroundSeparator = [[UIView alloc] initWithFrame:selectedBackgroundSeparator.frame];
selectedBackgroundSeparator.backgroundColor = backgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[backgroundView addSubview:backgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
Alternatively, you can use the default cell separator, and instead just add your own top and bottom separators to the selectedBackgroundView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *topSelectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, 0, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectOffset(topSelectedBackgroundSeparator.frame, 0, cell.frame.size.height)];
topSelectedBackgroundSeparator.backgroundColor = selectedBackgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[selectedBackgroundView addSubview:topSelectedBackgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for(id view in cell.containtView.subview) {
if([view isKindaOfClass:[UILabel class]])
UILabel* titleLabel = (UILabel*)view;
[titleLabel setTextColor:[UIColor whiteColor]]; // any you want
}
}
Rather than setting the cell's selectedBackgroundView to a clear view, would simply not allowing the cell to be highlighted when selected accomplish what you want? It will prevent the cell and separators from changing automatically based on the selection, but you will have to manage recognizing the tap gesture and highlighting the label text yourself.
Remove the selectedBackgroundView from your code.
You then need to implement tableView:shouldHighlightRowAtIndexPath: in your UITableViewDelegate:
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO; // do what's appropriate based on the indexPath
}
hello I am beginner in iOS I have created custom table view and I have Two array then I want to set both array data together in simultaneously one by one cell content in table view I show image which i want..
Please refer : http://i.stack.imgur.com/WIkaf.png
NSMutableArray *SearchPatientCode;
NSMutableArray *SearchPatientName;
UITableView *table_SearchPatient;
SearchPatientCode=[[NSMutableArray alloc]initWithObjects:#"PH230130420",#"PH230420321",#"Ph450362120", nil];
SearchPatientName=[[NSMutableArray alloc]initWithObjects:#"Rahul Sharma",#"Amit kumar",#"anil sharma", nil];
table_SearchPatient=[[UITableView alloc]initWithFrame:CGRectMake(130,40,170,250)style:UITableViewStylePlain];
table_SearchPatient.delegate=self;
table_SearchPatient.dataSource=self;
table_SearchPatient.layer.borderWidth = 2.0;
table_SearchPatient.layer.borderColor = [UIColor grayColor].CGColor;
[self.view addSubview:table_SearchPatient];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(tableView==table_SearchPatient)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
cell.textLabel.text =[NSString stringWithFormat:#"%# /n %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:10.0f];
// cell.detailTextLabel.text=[SearchPatientName objectAtIndex:indexPath.row];
}
return cell;
}
I am using this But this is not show as I want ...Solve this problem!!
Add New UILable with numberOfLines = 2 like bellow...
if you want to unlimited line with your content data then you can do it with set numberOfLines = 0 and set lineBreakMode = UILineBreakModeWordWrap
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(tableView==table_SearchPatient)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
UILabel *lbl = [[UILabel alloc]init];
lbl.text = [NSString stringWithFormat:#"%# /n %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:10.0f];
[lbl setFrame:CGRectMake(20, 2, 250, 35)];// set frame which you want
[lbl setBackgroundColor:[UIColor clearColor]];// set any color here
// lbl.lineBreakMode = UILineBreakModeWordWrap; // set it if you want to height of UILable with its content (Multiple line)
lbl.numberOfLines = 2;// Add this line // Set 0 if you want to multiple line.
[cell.contentView addSubview:lbl];
}
return cell;
}
You can use detail cell.detailTextLabel.text.
Or You can create a custom cell and add the labels to the cell like this
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
cell.textLabel.text = #"";
label_name_one = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)]autorelease];
[label_name_one setText:[SearchPatientCode objectAtIndex:indexPath.row]];
label_name_two = [[[UILabel alloc]initWithFrame:CGRectMake(92, 30, 170, 80)]autorelease];
[label_name_two setText:[SearchPatientCode objectAtIndex:indexPath.row]];
set the RectFrame according to your requirements.
Firstly change your cell style as UITableViewCellStyleValue2 then Use this code for multiline text in cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"MyIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:cellIdentifier];
[[cell textLabel] setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0f]];
}
[[cell detailTextLabel] setText:[NSString stringWithFormat:#"%# %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]]];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
Hope it helps you.
Also make sure you check out the free Sensible TableView framework. Given the arrays, the framework will automatically display all the values in the table view, and will even generate detail views where applicable. Saves me tons of time.
I'm adding two custom UILabel to a section of my UITableView in this way:
//in .h file:
NSArray *listaopzioni;
#property (nonatomic, retain) NSArray *listaopzioni;
//in .m file:
self.listaopzioni = [[NSArray arrayWithObjects:#"Strumenti",#"Help & Credits", nil] retain];
- (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] autorelease];
}
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
[slogan release];
}
}
All woks perfectly, but when I slide up and down the tableview (trying to cover the cells below the UINavigationBar) I get a strange effect: the text is overlapped just making each letter thicker.
What's wrong?
Method cellForRowAtIndexPath is called everytime the cell becomes visible.
That is why it creates labels everytime you scroll.
The solution is to put Label creation when you create the cell:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
[slogan release];
}
}
UITableViewCells (when used properly) get reused, which means after they've been created, they keep their created state, i.e. the label has been added to your cell. What you need to do is make use of this cell reuse to your advantage:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel slogan;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
slogan = [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.tag = 2121; // Any unique-to-the-cell, positive integer
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
} else {
slogan = [cell viewWithTag:2121]; // Must match slogan.tag
}
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
}
}
Is it possible to add a label to a UITableViewCell while reusing cells? This is the code I have been trying but when I scroll down and the cells get reused my label moves around. I just want the label in the very first row at all times, but I cannot figure this out for the life of me. I know I can not reuse the cells and it will work but I would like to reuse the cells. Please help me figure this out. Thank you very much...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if ([indexPath row] == 0) {
UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)];
[Label setText:#"Text"];
[cell addSubview:Label];
[Label release];
}
return cell;
}
I think I figured it out. I made the 1st row not reusable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellsToBeReused = #"CellsToBeReused";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellsToBeReused];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellsToBeReused] autorelease];
}
if ([indexPath row] == 0) {
UITableViewCell *first_Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)];
[Label setText:#"Text"];
Label.backgroundColor = [UIColor whiteColor];
[first_Cell.contentView addSubview:Label];
[Label release];
return first_Cell;
} else {
return cell;
}
}