I have UIButton in each cell of UITableView. When I touch it, its state is set to selected. But when I scroll table view so the button isn't visible, the state is set to normal. How can I do it that UIButton remain selected?
Thank you.
Edit: Here is the cellForRowAtIndexPath code:
- (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.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row < [messagesArray count]) {
Zprava *msgObj = [messagesArray objectAtIndex:indexPath.row];
int width = 0;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
width = 320;
} else {
width = 480;
}
CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);
CGSize requiredSize = [msgObj.mmessage sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
UIButton *background = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 300, requiredSize.height + 20)];
[background setBackgroundImage:[[UIImage imageNamed:#"balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal];
[background setBackgroundImage:[[UIImage imageNamed:#"selected-balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateSelected];
[background addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
background.tag = msgObj.msgID;
[[cell contentView] addSubview:background];
[background release];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 285, 15)];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setTextColor:[UIColor lightGrayColor]];
[dateLabel setNumberOfLines:0];
[dateLabel setTextAlignment:UITextAlignmentRight];
[dateLabel setText:msgObj.mdate];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setOpaque:NO];
[[cell contentView] addSubview:dateLabel];
[dateLabel release];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 275, requiredSize.height + 5)];
[messageLabel setFont:[UIFont systemFontOfSize:17]];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setNumberOfLines:0];
[messageLabel setText:msgObj.mmessage];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setOpaque:NO];
[[cell contentView] addSubview:messageLabel];
[messageLabel release];
}
return cell;
}
Save button states somewhere in your model separately from table view and set buttons state in cellForRowAtIndexpath: method again.
Related
Is that possible to use tableView cells like a button? In my app I want to have a table view which has 4 rows and in each row there will be labels. What I want is when I touch a row, the table tableview cell should react like a button (perform an action and background is highlighted)? Is that possible? How can I do that? Thanks.
very simple the delegate method will be called didSelectRowAtIndexPath there you can get which row is tapped in which section and you can perform further action.
for example
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0){
LoginFormViewController *vController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
vController = [[LoginFormViewController alloc] initWithNibName:#"LoginFormViewController_ipad" bundle:nil];
}else {
vController = [[LoginFormViewController alloc] initWithNibName:#"LoginFormViewController" bundle:nil];
}
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
}
You can implement this 'button' functionality in the tableView:didSelectRowAtIndexpath method in your UITableView delegate ;)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do something when pressed on an UITableViewCell
}
The cel will highlight, and your table view's delegate will receive a tableView:didSelectRowAtIndexPath: message anyway. You don't need anything extra to achieve what you have described.
- (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.selectionStyle=UITableViewCellSelectionStyleGray;
}
if(tableView.tag==1)
{
cell.userInteractionEnabled = NO;
cell.backgroundColor =[UIColor blackColor];
UIButton *Btn11=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn11 addTarget:self action:#selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[Btn11 setBackgroundImage:[UIImage imageNamed:#"small_img4.png"] forState:UIControlStateNormal];
Btn11.tag=indexPath.row;
Btn11.frame=CGRectMake(0, 6.5, 156, 74.5);
UILabel *Main_tblImg_Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 80,156, 31)];
[Main_tblImg_Label setBackgroundColor:[UIColor blackColor]];
//[Main_tblImg_Label setText:#"First Drive 2012 Mercedes Benz M class"];
//Main_tblImg_Label.textAlignment = UITextAlignmentCenter;
[Main_tblImg_Label setTextColor:[UIColor whiteColor]];
[Main_tblImg_Label setFont:[UIFont fontWithName:#"Helvetica" size:9]];
[Main_tblImg_Label setNumberOfLines:2];
[cell.contentView addSubview:Main_tblImg_Label];
[cell.contentView addSubview:Btn11];
UILabel *other_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(14, 80, 142, 29)];
[other_Lbl setText:#"First Drive 2012 \nMercedes Benz M class"];
[other_Lbl setBackgroundColor:[UIColor blackColor]];
[other_Lbl setTextColor:[UIColor whiteColor]];
[other_Lbl setFont:[UIFont fontWithName:#"Helvetica" size:9]];
[other_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other_Lbl];
[cell.contentView addSubview:Btn11];
//[cell.contentView addSubview:Main_tblImg_Label];
// [cell.contentView addSubview:Btn11];
//
UIButton *Btn22=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn22 addTarget:self action:#selector(ratingAction2:) forControlEvents:UIControlEventTouchUpInside];
[Btn22 setBackgroundImage:[UIImage imageNamed:#"Screen2.png"] forState:UIControlStateNormal];
Btn22.tag=indexPath.row;
Btn22.backgroundColor=[UIColor clearColor];
Btn22.frame=CGRectMake(165, 6.5, 156, 74.5);
UILabel *main_tblImg_Lbl2=[[UILabel alloc]initWithFrame:CGRectMake(165, 80, 156, 31)];
[main_tblImg_Lbl2 setBackgroundColor:[UIColor blackColor]];
//[main_tblImg_Lbl2 setText:#"Audi planning Mexican Assembly plant?"];
[main_tblImg_Lbl2 setTextColor:[UIColor whiteColor]];
//[main_tblImg_Lbl2 setFont:[UIFont fontWithName:#"times" size:1]];
[main_tblImg_Lbl2 setFont:[UIFont fontWithName:#"Helvetica" size:10]];
[main_tblImg_Lbl2 setNumberOfLines:2];
[cell.contentView addSubview:main_tblImg_Lbl2];
UILabel *other1_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(176, 80, 136, 31)];
[other1_Lbl setText:#"Audi planning Mexican Assembly plant?"];
[other1_Lbl setBackgroundColor:[UIColor blackColor]];
[other1_Lbl setTextColor:[UIColor whiteColor]];
[other1_Lbl setFont:[UIFont fontWithName:#"Helvetica" size:10]];
[other1_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other1_Lbl];
[cell.contentView addSubview:Btn11];
[cell.contentView addSubview:Btn22];
}
return cell
}
I have created a custom tableView with labels and buttons. On a button click in a cell i want to change the label text in that cell only. My problem is when i click button corresponding labels in other cell also changes. Here is my code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: CellIdentifier] autorelease];
UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100, 20)];
labelOne.tag = 100;
labelOne.backgroundColor=[UIColor clearColor];
labelOne.font=[UIFont systemFontOfSize:14];
[cell.contentView addSubview:labelOne];
[labelOne release];
UIButton *minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
minusbutton.backgroundColor=[UIColor clearColor];
[minusbutton addTarget:self action:#selector(DecreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[minusbutton setTitle:#"-" forState:UIControlStateNormal];
[minusbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[minusbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:minusbutton];
[minusbutton release];
UILabel *quantity = [[UILabel alloc]initWithFrame:CGRectMake(170, 5, 20, 20)];
quantity.tag = 103;
quantity.backgroundColor=[UIColor clearColor];
quantity.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:quantity];
[quantity release];
UIButton *addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
addbutton.backgroundColor=[UIColor clearColor];
[addbutton addTarget:self action:#selector(IncreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[addbutton setTitle:#"+" forState:UIControlStateNormal];
[addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[addbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:addbutton];
[addbutton release];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)];
labelTwo.tag = 101;
labelTwo.backgroundColor=[UIColor clearColor];
labelTwo.font=[UIFont systemFontOfSize:14];
labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1];
[cell.contentView addSubview:labelTwo];
[labelTwo release];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 230, 30)];
label3.tag = 102;
label3.backgroundColor=[UIColor clearColor];
label3.numberOfLines=2;
label3.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
NSString *ss2=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]tit] ];
labelOne.text = ss2;
UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
labelTwo.text = [[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]price] ];
UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102];
label3.text=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]desc ] ];
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",qty];
}
return cell;
}
Initially qty=0
now i want that when i click addbutton, qty of that cell increases. here is my button click event
-(void)IncreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
//NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
UILabel *label4 = (UILabel *) [currentCell.contentView viewWithTag:103];
label4.text=[[[NSString alloc]initWithFormat:#"%d",qty++] ;
//NSLog(#"%d",row);
[ticketTable reloadData];
}
But the problem is when i click the button,qty of all cell increases. Please guide me how to do it. Any help will be appreciated.
Set condition like this..
set on global Variable NSUInteger myRow.
myRow = indexPath.row;//Set Selected index path.
in cellForRowAtIndexPath set condition.
if(myRow == indexPath.row)
{
NSUInteger newqty = qty + 1;
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",newqty];
}
esle
{
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",qty];
}
and set some logic for that.
when you call
[ticketTable reloadData];
all your data reload and your table is initialized again
UITable magic is in reuse of a few allocated cells (just the minimus number of cell required: the viewable cells in a time on screen) and then you should just reuse them with new data setting when you scroll, typically using NSArray in your table data delegate...
and: where qty is defined? it seems it's a global variable, not one for every single cell...
you'd better to read some apple example code or some tutorial
I´m using the method cellForRowAtIndexPath to draw cells with 2 labels. When i reuse a cell the label i had before in that cell isn´t erased, so i get two labels overlapping because i´m reusing a cell. This is weird cause only the label title gets overlapped, the other doesn´t.
So what i did to fix this, is that i´m always creating a new cell. Which isn´t appropriate but solves the problem... Anyone had a similar bug and manage to fix it? Cause i just made a workaround...
//Desenhado por codigo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
UILabel *label2 = nil;
UIColor *green = [UIColor colorWithRed:0 green: 0.77 blue:0 alpha:1];
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
// if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
//Descrição
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setFont:[UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:14.0]];
[label setTag:1];
[label setTextColor:green];
[label setBackgroundColor:[UIColor clearColor]];
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"barra_cinza_lista.png"]];
[[cell contentView] addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectZero];
[label2 setLineBreakMode:UILineBreakModeWordWrap];
[label2 setMinimumFontSize:FONT_SIZE];
[label2 setNumberOfLines:0];
[label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label2 setFont:[UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:14.0]];
[label2 setTag:2];
[label2 setTextColor:[UIColor whiteColor]];
[label2 setBackgroundColor:[UIColor clearColor]];
[[cell contentView] addSubview:label2];
}
NSString *text = [self getTableViewRow:tableView index:indexPath];
NSString *title = #" %#";
if(tableView == myTableView1)
title = [NSString stringWithFormat:title, [_titlesResults objectAtIndex:indexPath.row]];
else
title = [NSString stringWithFormat:title, [_titlesVision objectAtIndex:indexPath.row]];
CGFloat widthMax = CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2);
CGSize constraint = CGSizeMake(widthMax, 20000.0f); //Tamanho máximo permitido de largura e altura
CGSize size1 = [title sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; //Tamanho ocupado pelas letras
CGSize size2 = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; //Tamanho ocupado pelas letras
if (!label)
label = (UILabel*)[cell viewWithTag:1];
if (!label2)
label2 = (UILabel*)[cell viewWithTag:2];
[label setText:title];
[label setFrame:CGRectMake(0, 0, 320, MAX(size1.height, 44.0f))];
[label2 setText:text];
[label2 setFrame:CGRectMake(CELL_CONTENT_MARGIN, 3 * CELL_CONTENT_MARGIN + size1.height, widthMax, MAX(size2.height, 44.0f))];
return cell;
}
At the beginning of the method, enumerate through all the UIView objects in [cell.contentView subviews] and get rid of them, like so:
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
This works because all UI elements inherit from UIView and thus can be removed as if they were.
first, uncomment this line:
// if (cell == nil)
if you don't, you are simply creating and allocating a new cell every time you scroll, and never release the old ones...
I have UITableViewCell with UILabel and UISwitch. By default all UISwitch is set to off.
Once I will turn on the switch and then scroll through table the switch value is set to default again,i.e.Off
Below is the code which I have used:
- (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] autorelease];
}
if (indexPath.row == 0) {
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
lbl1.text = #"Some Text";
[cell addSubview:lbl1];
switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)];
[switch setOn:NO];
switch.tag = 1;
[switch addTarget:self action:#selector(switchTapped:) forControlEvents:UIControlEventChanged];
[cell addSubview:switch];
}
}
// Below is my switchTapped method:
- (void) switchTapped: (id)sender {
UISwitch *tapSwitch = (UISwitch *)sender;
switch (tapSwitch.tag) {
case 1:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
case 2:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
}
Am I doing anything wrong over here?
Thank You.
You're using really nasty code which regenerates the cell each time it's needed:
if(cell != nil)
{
cell = nil;
}
if (cell == nil)
{
...
}
Do you bind the state of your switch to some retained object (e.g. Item model object, where single cell reflects an Item)?
This is how I write my cell drawing code:
Basically, within my if(cell == nil) { ... } I do all the "initWithFrame". Anything outside that, I simply set the value such as label text. Don't do the initWithFrame outside the if(cell == nil) {...} block of code.
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *reusableCell = #"reusableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell] autorelease];
thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)];
feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)];
postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)];
description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)];
// setting tag reminders so we can identify the element to replace
[thumbnail setTag:1];
[feedTitle setTag:2];
[postDate setTag:3];
[description setTag:4];
[[cell contentView] addSubview:thumbnail];
[[cell contentView] addSubview:feedTitle];
[[cell contentView] addSubview:description];
[[cell contentView] addSubview:postDate];
[thumbnail release];
[feedTitle release];
[description release];
[postDate release];
}
thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1];
feedTitle = (UILabel *)[[cell contentView] viewWithTag:2];
postDate = (UILabel *)[[cell contentView] viewWithTag:3];
description = (UILabel *)[[cell contentView] viewWithTag:4];
[feedTitle setBackgroundColor:[UIColor clearColor]];
[feedTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16]];
[feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
[description setBackgroundColor:[UIColor clearColor]];
[description setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]];
[description setNumberOfLines:2];
[description setLineBreakMode:UILineBreakModeWordWrap];
[postDate setBackgroundColor:[UIColor clearColor]];
[postDate setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]];
[thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:#"thumb"]];
[feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:#"title"]];
[description setText:[[items objectAtIndex:[indexPath row]] objectForKey:#"summary"]];
// Format date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:#"date"]]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
if([feedList contentOffset].y < -50)
{
shouldUpdate = TRUE;
[activityIndicator stopAnimating];
[feedList setContentOffset:CGPointMake(0, -30) animated:NO];
[self loadData];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)];
[loadingLabel setText:#"Loading New Data"];
[loadingLabel setTextAlignment:UITextAlignmentCenter];
[loadingLabel setBackgroundColor:[UIColor clearColor]];
[loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
[loadingLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)];
[reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[reloadingSpinner startAnimating];
[reloadingSpinner setHidesWhenStopped:YES];
[feedList addSubview:reloadingSpinner];
[feedList addSubview:loadingLabel];
}
return cell;
}
I am developing a project in which there is one requirement that in a table view when I tapped a row there should be one row added below that row and also I want that row to be customized as I want to add a text view in that row.
Please tell me how can I do this.
Any suggestions would be highly appreciated.
My current code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
UIImageView *imgViewBackground=(UIImageView *)[cell.contentView viewWithTag:20];
UIImageView *imgView=(UIImageView *)[cell.contentView viewWithTag:21];
UILabel *label1=(UILabel *)[cell.contentView viewWithTag:22];
UILabel *label2=(UILabel *)[cell.contentView viewWithTag:23];
UILabel *label3=(UILabel *)[cell.contentView viewWithTag:24];
[imgViewBackground setImage:[UIImage imageNamed:#"bba3.png"]
[imgView setImage:[UIImage imageNamed:#"picdefault.png"]];
[label1 setText:[self.usernameArray objectAtIndex:indexPath.row]];
[label2 setText:[NSString stringWithFormat:#"%#",[self.level1Array objectAtIndex:indexPath.row]]];
[label3 setText:[self.ageArray objectAtIndex:indexPath.row]]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if ([identifier isEqualToString:#"Cell"]) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 300, 98)];
imageView.tag=20;
[imageView setImage:[UIImage imageNamed:#"bba2.png"]];
[cell.contentView addSubview:imageView];
[imageView release];
UIImageView *imagePicView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 20, 60, 60)];
imagePicView.tag=21;
[cell.contentView addSubview:imagePicView];
[imagePicView release];
UILabel *namelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 20, 200, 20)];
namelabel.backgroundColor=[UIColor clearColor];
namelabel.font=[UIFont systemFontOfSize:15];
namelabel.tag=22;
[namelabel setTextAlignment:UITextAlignmentLeft];
[namelabel setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:namelabel];
[namelabel release];
UILabel *activitylabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 20)];
activitylabel.backgroundColor=[UIColor clearColor];
activitylabel.font=[UIFont italicSystemFontOfSize:14];
activitylabel.tag=23;
[activitylabel setTextAlignment:UITextAlignmentLeft];
[activitylabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:activitylabel];
[activitylabel release];
UILabel *agelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 60, 200, 20)];
agelabel.backgroundColor=[UIColor clearColor];
agelabel.font=[UIFont systemFontOfSize:13];
agelabel.tag=24;
[agelabel setTextAlignment:UITextAlignmentLeft];
[agelabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:agelabel];
[agelabel release];
}
return cell;
}
Look at the WWDC 2011 developer video for UITableView changes and tips - in there they describe how to do exactly that. Sadly there's not been any sample code I've ever been able to find, but they document the approach pretty well in the talk.