#implementation TestTableViewCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.selectedBackgroundView = [[UIView alloc] init];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if(highlighted || self.selected)
{
self.labelTest.tintColor = self.tintColor;
self.labelTest.text = #"HighLighted";
}
else
{
self.labelTest.tintColor = [UIColor redColor];
self.labelTest.text = #"Not HighLighted";
}
}
#end
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
tableView.separatorColor = [UIColor redColor];
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"testCell"];
if (indexPath.row == 5) {
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
return cell;
}
This is my code. If the row is 5 programmatically select the cell using selectRowAtIndexPath. When I do this, the tableseperator is missing in 7.1 But the same code works fine in 7.0
Not sure why the separator is missing in 7.1
Actually the problem was setting the selectedbackground. It works fine in iOS7 but have some issues in 7.1
Related
I have UITableView, with CustomCell. In CustomCell there is UICollectionView with CustomUICollectionViewCell.
Everything is created in the way, according to UICollectionView inside CustomTableCell.
Then, let's say I have 12 rows. On the beginning I can see 6 of them.
In CustomUICollectionViewCell, labels are fine, but when I scroll down, to see other cells, CustomUICollectionViewCells labels are not as expected. They are the same as first 6 rows, which are now scrolled up.
Where should I refresh CustomUICollectionViewCells label after scroll?
Example view:
Before scrolling:
After scrolling:
However when debugging, when assigning data to cell, everything is assigned fine.
All code is in the link as it's also my question.
Seems that it's duplicating rows. I guess that it should be reloaded(?).
Added prepareForReuse method, as advised, but didn't help. Now my CustomCellClass looks:
#implementation TemperatureTableViewCell
NSNumberFormatter *decimalStyleFormatter;
- (void)awakeFromNib
{
[super awakeFromNib];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(50, 50)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
[self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:#"TemperatureItemCollectionCell"];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.contentView addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.collection.count;
}
-(void)collectionView:(CollectionView *)collectionView willDisplayCell:(TemperatureItemCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
TemperatureSensor *temp = self.collection[indexPath.item];
cell.temperatureValue = temp.temperature.stringValue;
cell.tempValTempCollViewCell.text = [NSString stringWithFormat:#"%#°C", cell.temperatureValue];
cell.sensorName.text = temp.sensorName;
cell.imageTempCollViewCell.image = [TemperatureImageHelper getTemperatureIcon:temp];
[self.collectionView reloadData];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TemperatureItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"TemperatureItemCollectionCell" forIndexPath:indexPath];
return cell;
}
- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath {
self.collectionView.delegate = dataSourceDelegate;
}
#end
And my CustomCollectionViewCell:
#implementation TemperatureItemCollectionViewCell
-(void)awakeFromNib {
[super awakeFromNib];
}
-(void)prepareForReuse {
[super prepareForReuse];
self.imageTempCollViewCell.image = nil;
self.tempValTempCollViewCell.text = nil;
self.sensorName.text = nil;
self.temperatureValue = nil;
}
#end
My TableViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_tempSensorsDictionary == nil)
return 0;
else {
NSArray *allSensorKeys = [_tempSensorsDictionary allKeys];
return [allSensorKeys count];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(TemperatureTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setCollectionViewDelegate:self indexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
NSArray *nodeNumbers = [_tempSensorsDictionary allKeys];
TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath: indexPath];
cell.collection = [_tempSensorsDictionary objectForKey:nodeNumbers[indexPath.row]];
//NSArray *nodeNumbers = [_tempSensorsDictionary allKeys];
if([[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]] != nil &&
![[[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]] isEqualToString:#""]) {
NSString *name = [NSString stringWithFormat:#"%# (%#)", [[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]], nodeNumbers[indexPath.row]];
cell.unitNameLabel.text = name;
} else {
cell.unitNameLabel.text = nodeNumbers[indexPath.row];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 85.0;
}
Because UITableViewCells are reused for performance, caching can sometimes occur. In your TemperatureTableViewCell class you should override prepareForReuse and reset its properties. In your case you should clear the data you are populating it with. I would also keep prepareForReuse in TemperatureItemCollectionViewCell to avoid anymore issues.
- (void)prepareForReuse
{
[super prepareForReuse];
//Reset properties here.
self.collection = nil;
[self.collectionView reloadData];
}
I think this is what's happening. Inside of your child cell (I think it's collectionViewCell) you should reset all the values of the ui elements. Basically you are reusing one cell and you neeed to reset the values of the text or the images on it.
override func prepareForReuse() {
super.prepareForReuse()
titleLabel1.text = nil
image1.image = nil
...
}
I've created a custom UITableViewCell and i need a bottom line separator for each cell then i put the separator in init method of my custom tableViewCell
This is my TableView code :
#import "Telegram_TableViewController.h"
#import "Custom_TableViewCell.h"
#interface Telegram_TableViewController ()
#end
#implementation Telegram_TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.array =[[NSMutableArray alloc] init];
for (int i=0; i<200; i++) {
[self.array addObject:[NSString stringWithFormat:#"item %i" ,i]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.array count] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *Identifier = #"CELL";
Custom_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
cell = [[Custom_TableViewCell alloc] init];
UILabel *txt = [[UILabel alloc]init];
txt.text = [NSString stringWithFormat:#"Item " ] ;
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
return cell;
}
and my custom TableViewCell code :
#import "Custom_TableViewCell.h"
#implementation Custom_TableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(id)init
{
self = [super init];
if (self) {
UIEdgeInsets insets = self.separatorInset;
self.separatorInset = UIEdgeInsetsMake(0.0, 0.0,insets.bottom + 6.0, 0.0);
self.separatorInset = insets;
}
return self;
}
There is no error but it doesn't work. How can i fix it?
Thanks in advance.
Clean init method in Custom_TableViewCell and see if this helps.
In Telegram_TableViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *Identifier = #"CELL";
Custom_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
cell = [[Custom_TableViewCell alloc] init];
UILabel *txt = [[UILabel alloc]init];
txt.text = [NSString stringWithFormat:#"Item " ] ;
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setSeparatorColor:[UIColor blackColor]];
return cell;
}
And make sure the color of the separator is different than the color of the cell itself.
Use this code in your cell,s .h file
#property (nonatomic,strong) UIView* cellSeperatorView;
and in .m file
-(UIView *)cellSeperatorView{
if (!cellSeperatorView) {
self.cellSeperatorView = [[UIView alloc] initWithFrame:CGRectZero];
[self.cellSeperatorView setBackgroundColor:[UIColor lightGrayColor]];
[self addSubview:self.cellSeperatorView];
}
return cellSeperatorView;
}
-(void)prepareForReuse{
self.cellSeperatorView = nil;
}
-(void)layoutSubviews{
[super layoutSubviews];
[self.cellSeperatorView setFrame:CGRectMake(0, 0, 500, 1)];
}
and call this method in your tableview class in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)..
UITableViewCellClass *cell = (UITableViewCellClass *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.cellSeperatorView.hidden = (indexPath.row == ([self.yourtableViewArray numberOfRowsInSection:indexPath.section]));
and in you viewdidload method add this
[self.yourtableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
i think this can help you for making separator..
You'd better register in the method viewDidLoad when you want to custom cell. [self.tableView registerClass:[Custom_TableViewCell class] forCellReuseIdentifier:Identifier];
What is cell = [[Custom_TableViewCell alloc] init];? You are not reusing the cell, but initiate every time. Just do not write this line.
The right way to overwrite the init method of tableViewCell to is rewrite - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
Now you can see the tableView correctly, but I do not understand a bottom line separator, just write what you want in the method initWithStyle.
I want to make paging swipe left and right using UIScrollView after view detailController.
First, main.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OZDetailViewController *detailViewController = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
detailViewController.arrDetailNews = [_arrNews objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
OZDetailViewController *arrNewsAll = [[OZDetailViewController alloc] initWithNibName:#"OZDetailViewController" bundle:nil];
arrNewsAll.allNewsArray = _arrNews;
[self.navigationController pushViewController:arrNewsAll animated:YES];
}
When I selected content in tableviewcell, arrDetailNews can loaded in method viewDidLoad() and cellForRowAtIndexPath(). But arrNewsAll cannot loaded in method cellForRowAtIndexPath().
This is my detailViewController.h:
#property (nonatomic, copy) NSArray *allNewsArray;
And detailViewCOntroller.m:
#synthesize allNewsArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"dataArray: %#", allNewsArray);
}
- (int)numberINSectionsInTableView: (UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"dataArrayCell: %#", allNewsArray);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"New Cell"];
}
if(indexPath.row != self.arrDetailNews.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor whiteColor];
[cell addSubview:line];
}
tableView.allowsSelection = NO;
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
if (indexPath.row==0) {
cell.textLabel.text = #"1st";
}
if (indexPath.row==1) {
cell.textLabel.text = #"2nd";
}
if (indexPath.row==2) {
cell.textLabel.text = #"3rd";
}
return cell;
}
If allNewsArray can loaded in cellForRowAtIndexPath() I can continue next step for paging with UIScrollView. Note, numberOfRowsInSection I set to 4 because I need 4 rows (custom view).
Assuming you setup your delegate & dataSource outlets correctly from xib/storyboard, you still need to specify the number of rows per section (or number of sections).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return allNewsArray.count;
}
Alternatively, the method for number of sections is:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
I have a cell with a UIView with a white backgroundColor. I want to change background color with an animation. I use this method in -tableView:tableView didSelectRowAtIndexPath:indexPath :
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
} completion:NULL];
The animation begin correctly, but at the end, the UIView becomes white again.
How can I fix that ?
edit: full code
#pragma mark - Table view
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 71;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _arFiles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RecentCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
((UILabel*)[cell viewWithTag:2]).text = [[_arFiles objectAtIndex:indexPath.row]objectForKey:KEY_FILE_NAME];
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 10);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = ((UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath]);
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
} completion:NULL];
}
subclassing the cell may work as u expected, hear is the code try it out in custom cell
do somthing like below
//in CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView *view = [[UIView alloc]initWithFrame:CGRectZero];//your view
view.tag = 100;
[self addSubview:view];//add to cell
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *view = [self viewWithTag:100];
if(selected)
{
//animation when selected
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
view.backgroundColor = [UIColor orangeColor];
} completion:NULL];
}
else
{
//this is for when deselect if u want color in same sate then set it
}
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
UIView *view = [self viewWithTag:100];//get the view
view.frame = CGRectMake(5, 5, 100, 30);//setting the frame
}
in your controller just use this custom cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//dont add any animation hear becz u already added in custom cell
// [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
// ([cell viewWithTag:100]).backgroundColor = [UIColor orangeColor];
// } completion:NULL];
}
I have solve my problem:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = ((UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath]);
[self performSelector:#selector(launchAnimation:) withObject:cell afterDelay:0.1];
}
-(void)launchAnimation:(UITableViewCell*)cell{
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionOverrideInheritedCurve animations:^(void){
([cell viewWithTag:20]).backgroundColor = [UIColor orangeColor];
} completion:NULL];
}
I am very new to Objective-C. I just searched and tried to learn about UITableView.
I have created this UITableView. instead of "a" in all the rows i want it to be in a sequence like "a b c d..." and if i increase the no of rows it should scroll. its not scrolling so here is my code.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
cell.textLabel.text = #"a";
return cell;
}
#end
set table view Delegates in IB or if u created Programmatically then in viewDidLoad Take one array fill it Dynamically or Statically.Give array count in NumberOfRowsInsection method and reload table view at appropriate place
in your cellForRowAtIndexPath: add these lines
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
Check this tutorial, I'm sure it will help you
UItableView Guide
and for scrolling, You don't need to do anything yourselves, as the number of cells increases beyond the provided size, it automatically becomes scrollable and make sure that scrolling is enabled in your UITableView attribute inspector, check the image
Happy Coding
Regards
Use below code.....define alphabetarr in globally then use it in code...
- (void)viewDidLoad
{
alphabetarr = [[NSArray alloc] initWithObjects:#"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", nil];
CGRect frame = self.view.frame;
UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
tableview.backgroundColor = [UIColor cyanColor];
tableview.separatorColor = [UIColor blackColor];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[super viewDidLoad];
// 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 [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];
return cell;
}