UITableView not display text on devices - ios

I develop application contains UITableView (not Controller) and it was added programmatically. I didn't use any nib file. and found out that in simulator work fine but's when I build on device Table shown up but didn't see any text on it.
Datasource and delegate is already connected.
here some code
if (kPortrait) {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(568, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
} else {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(824, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
}
[optionTV setAutoresizesSubviews:YES];
[optionTV setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[optionTV setDataSource:self];
[optionTV setDelegate:self];
optionTV.alpha = 0.0f;
optionTV.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.50];
[self.view addSubview:optionTV];
[UIView animateWithDuration:0.5f animations:^{
trafficBT.transform = CGAffineTransformMakeTranslation(-200, 0);
optionTV.alpha = 0.8;
}];
for delegate method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"tableView Data num row: %d", [optionArray count]);
return [optionArray count];
}
- (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];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
if (kIsIPhone) {
cell.textLabel.font = [UIFont systemFontOfSize:18];
} else {
cell.textLabel.font = [UIFont systemFontOfSize:20];
}
cell.textLabel.text = [optionArray objectAtIndex:indexPath.row];
return cell;
}

Related

TableViewCell is piled up and appear again and again

I implemented TableView with ADLively TableView.
But if I scroll tableView, the cell's texts become to be piled up again and again....
Using "cell.textLabel" is good, adding UILabel is not good than that but if I use "cell.textLabel", I can't resize width of textLabel.
(I want to add UIImageView on the left and right side of text)
So now, I use the way to add UILabel.
What should I do?
Here is the code.
- (void)viewDidLoad {
CGRect rect = CGRectMake(0, 50, 320, self.view.frame.size.height-150);
self.tableView = [[ADLivelyTableView alloc]initWithFrame:rect style:UITableViewStylePlain];
self.tableView = (ADLivelyTableView *)self.tableView;
self.tableView.initialCellTransformBlock = ADLivelyTransformFan;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview: self.tableView];
//self.tableView.backgroundColor = [UIColor blueColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.section count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];
if (cell == nil){
myCellView = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[myCellView.contentView addSubview:tableTitleLabel];
}
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
[(UILabel *)[cell.contentView viewWithTag:1] setText:[set objectAtIndex:0]];
return cell;
}
- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[cell.contentView addSubview:tableTitleLabel];
}
It is not a good idea to keep adding subviews in cell on scroll but if you do not want to change the code that you have already written, use the following to remove all subviews of that cell before you call you updateCell method.
for (UIView *view in [cell subviews])
{
[view removeFromSuperview];
}

Programiticaly Multiple Columns in Tableview ios

I am trying to create a table with multiple columns, I am using array of cells.
Following is my code, I get single columns every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
CGFloat x = 0.0f;
UIView *lastCol = [[cell columnCells] lastObject];
if (lastCol) x = lastCol.frame.origin.x + lastCol.frame.size.width;
for (int i = 0; i < 2; i++) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, i, 40.0f)] ;
UIView *gridCell = l;
CGRect f = gridCell.frame;
f.origin.x += x;
gridCell.frame = f;
[cell.contentView addSubview:gridCell];
CGFloat colWidth = [self widthForColumn:i];
x += colWidth + 1.0f;
[[cell columnCells] addObject:gridCell];
}
for (int i = 0; i < 2; i++) {
UILabel *l = (UILabel*)[cell columnCells][i];
l.text =self.department[indexPath.row];
}
return cell;
}
How about this?
// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
if (i==0) {
mView.backgroundColor = [UIColor redColor];
} else if (i==1) {
mView.backgroundColor = [UIColor greenColor];
} else if (i==2) {
mView.backgroundColor = [UIColor blueColor];
}
[cell addSubview:mView];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Thanks to Fahim, I just modified his code and got what I wanted.
Here's the exact requirement which I was looking for.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];
[label setText:self.department[indexPath.row]];
[cell addSubview:mView];
[mView addSubview:label];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

UICustom cell not showing labels in iOS 7

I have added custom UItableViewCell with labels. Labels are visible iOS 6 but not in iOS 7.
-(void)layoutSubviews{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(90.0f ,-5.0f, 200.0f, 50.0f);
self.imageView.frame = CGRectMake(3.0f , 2.0f, 75.0f, 55.0f);
}
-(void)drawRect:(CGRect)rect{
self.imageView.layer.cornerRadius = 5;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderColor = [UIColor colorWithRed:0.0 green:0.4 blue:0.7 alpha:0.9].CGColor;
self.imageView.layer.borderWidth = 2;
[self.textLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
self.textLabel.backgroundColor = [UIColor clearColor];
[[UIColor grayColor] set];
[videoDurationTextLabel drawInRect:CGRectMake(90, 30, 190, 10) withFont:[UIFont fontWithName:#"Helvetica" size:12]];
[videoFileSizeLabel drawInRect:CGRectMake(170, 30, 190, 15) withFont:[UIFont fontWithName:#"Helvetica" size:12]];
}
TableViewController class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"LazyTableCell";
VideoCustomCell *cell = [self.tblView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[VideoCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = videoRecord.videoTitle;
cell.videoFileSizeLabel = [NSString stringWithFormat:#"%.4lfMb",totalSpaceInMB];
[cell.imageView setImage:[UIImage imageWithData: imgData]];
cell.videoDurationTextLabel = videoRecord.duration;
}
Try this code.
- (void) layoutSubviews {
[super layoutSubviews];
CGRect cvb = self.contentView.bounds;
CGRect imf = self.imageView.frame;
imf.origin.x = cvb.size.width - imf.size.width;
self.imageView.frame = imf;
CGRect tf = self.textLabel.frame;
tf.origin.x = 5;
self.textLabel.frame = tf;
}
- (void) viewDidLoad {
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:#"CustomTableCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"CustomTableCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.titleLabel.text ="test text";
[cell.imageView setImage:[UIImage imageWithData: imgData]];
return cell;
}
You have to name your custom cell using your cell identifiers name.
As i see in your UITableViewCell code, your cell identifier name is LazyTableCell. Rename your cell to have the same name.

Flashing background on UITableview

I am trying to make quiz program. I used to UITableview for answers. How can i make to flashing on background when user click the correct answer? Is it possible?
Here is the some part of my project;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
//cell=[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum] )
{
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *text = [answers objectAtIndex:indexPath.row];
NSData *data = [text dataUsingEncoding: [NSString defaultCStringEncoding] ];
cell.textLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ];
//cell.textLabel.text=[answers objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
tableView.backgroundColor=[UIColor whiteColor];
cell.backgroundColor=[UIColor whiteColor];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvatica" size:15]];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
CGRect rect = cell.frame;
UIView * view = [[UIView alloc] init];
UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ListGray"] ];
BOOL selx;
if (answerType==2)
selx = [self isSelected:indexPath];
else
selx = [lastIndexPath isEqual:indexPath];
if (selx) {
back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"aaaa.png"] ];
cell.textLabel.textColor = [UIColor whiteColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
} else {
cell.accessoryView = nil;
if (lastIndexPath != nil){
cell.textLabel.textColor = [UIColor blackColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
back = nil;
}
}
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
tableView.allowsSelection = NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if (answerType==3) {
NSString *secilenAnswerID;
secilenAnswerID= [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
for (int a=0; a<[answers count]; a++) {
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:a inSection:0]];
cell. accessoryType= UITableViewCellAccessoryNone;
[selectedCells removeObject:rowNsNum];
selectedCells=[[NSMutableArray alloc]init];
}
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
isRadioSelected=TRUE;
radioAnswer = [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
[selectedCells addObject:rowNsNum];
lastIndexPath = indexPath;
[tableView reloadData];
}
}
Sorry for my bad english. Thank you for your interest.
You can add a background view into your UITableViewCell with a backgroundColor. Then in your tableView: didSelectRowAtIndexPath: you can animate the hidden or alpha with the animation APIs in UIView. When the animation finishes you can animate hidden to TRUE or alpha back to 0 to get the flash effect.
Here is my question's solution. I took advice from Stephen Johnson:)
[UIView animateWithDuration:0.1f delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[UIView setAnimationRepeatCount:3];
cell.alpha = 0;
} completion: ^(BOOL finished) {
cell.hidden = YES;
[UIView animateWithDuration:0.1 animations:^{
cell.alpha = 1;
} completion: ^(BOOL finished) {
cell.hidden = NO;
}];
}];

strike on the uilabel disappears in uitableview did select cell

1)i want to create a striked text so i have used thet ext frame and i have created it and keep on the lbal it works fine when i have used it in the table cell when i am selecting the cell the strike on the label disaperared
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
tableView1.backgroundColor = [UIColor clearColor];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C.png"]];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C_h.png"]];
UILabel *price1_lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,80,100,26)];
price1_lbl.text = #"label";
price1_lbl.textColor = [UIColor grayColor];
price1_lbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:(13.0)];
price1_lbl.backgroundColor = [UIColor clearColor];
NSString *string =price1_lbl.text;
CGSize stringSize = [string sizeWithFont:price1_lbl.font];
CGRect buttonFrame = price1_lbl.frame;
CGRect labelFrame = CGRectMake(buttonFrame.origin.x ,
4+buttonFrame.origin.y + stringSize.height/2,
stringSize.width, 1);
UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
lineLabel.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:price1_lbl];
[cell.contentView addSubview:lineLabel];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
secondviewcontroller *obj=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
}
Try to add the strikethrough line directly to the label with the text (price1_lbl). If that doesn't work you may want to give this lib a try.
UPDATE:
You can use this code:
CGSize size = [price1_lbl.text sizeWithFont:[UIFont systemFontOfSize:16.0f]];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0 , label.frame.size.height / 2, size.width, 1)];
line.backgroundColor = [UIColor blackColor];
[price1_lbl addSubview:line];

Resources