I have added to some rows from table view an TextField like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row==1)
{
code=[[UITextField alloc] init];
code.frame = CGRectMake(200,10,80,50);
code.textColor=[UIColor grayColor];
code.text=#"515800";
[cell.contentView addSubview:code];
}
if(indexPath.row==2)
{
ville=[[UITextField alloc] init];
ville.frame = CGRectMake(200,10,80,50);
ville.textColor=[UIColor grayColor];
ville.text=#"Paris";
[cell.contentView addSubview:ville];
}
if(indexPath.row==4)
{
nomdepartenaire=[[UITextField alloc] init];
nomdepartenaire.frame = CGRectMake(200,10,80,50);
nomdepartenaire.textColor=[UIColor grayColor];
nomdepartenaire.text=#"Alliantis Ttttt";
[cell.contentView addSubview:nomdepartenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [listData objectAtIndex:indexPath.row]] ;
return cell;
}
The problem is that the keyboard doesn't appear if I want to change the text from TextField. Why?
Most probably there is something in the way of the text field and that makes it not receiving the touch event. Adding the entire cellForRowAtIndexPath would probably help us understand better what's happenning. Also, you should add the texfield and other custom elements to cell.contentsView, not directly to the cell.
Related
I am making an iphone app in which i want that when app load it should just one cell with textfield when user enter any data in first cell it should new cell with entered data and first cell should be empty as editable.
I am making but it shows same value in above cell also.
here is the code
-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.backgroundColor = [UIColor clearColor];
}
if (indexPath.section==0) {
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(13, 6, 250, 26)];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#" "];
tagInputField.textColor =[UIColor grayColor];
[cell addSubview:tagInputField];
cell.textLabel.text=#"";
return cell;
}
if (indexPath.section==1) {
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(230, 8, 18, 18)];
//crossButton.backgroundColor = [UIColor purpleColor];
crossButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Cross.png"]];
[cell addSubview:crossButton];
cell.textLabel.font =[UIFont systemFontOfSize:13];
cell.textLabel.textColor =[UIColor grayColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
}
Try this code:
-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.backgroundColor = [UIColor clearColor];
}
for (UIView *subView in cell.subviews)
{
if (subView.tag == 2 || subView.tag == 22)
{
[subView removeFromSuperview];
}
}
if(indexPath.section==0){
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(13, 6, 250, 26)];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#" "];
tagInputField.textColor =[UIColor grayColor];
[cell addSubview:tagInputField];
cell.textLabel.text=#"";
return cell;
}
if(indexPath.section==1) {
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(230, 8, 18, 18)];
*crossButton.tag = 22; //use a tag value that is not used for any other subview
//crossButton.backgroundColor = [UIColor purpleColor];
crossButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Cross.png"]];
[cell addSubview:crossButton];
cell.textLabel.font =[UIFont systemFontOfSize:13];
cell.textLabel.textColor =[UIColor grayColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
}
I was trying to implement a UILabel into a cell and what I get is overlapping of some values when I scroll the table up and down a couple of times. I work with ARC so there is no release when I want, so my question is : What's the right way of implementing a Label into a tableView cell?
Here is how it looks
- (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];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[cell addSubview:cellLabelS1];
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
You should add a label to the cell only if you don't have one.If you reuse the cells on the second pass you add it again.
So my advice is to set a tag to the label and try to see if the cell contentView bass already the label. If not create and add it.
UILabel *myLabel = (UILabel *)[cell.contentView viewWithTag:2002];
if(!myLabel){
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
myLabel.tag = 2002;
[cell.contentView addSubview:myLabel];
}
myLabel.text = #"my new text";
- (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 *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0,cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.tag = 200;
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cell addSubview:cellLabelS1];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = (UILabel*)[cell viewWithTag:200];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
may this will help you....
Your problem is in these lines:
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
When you get a reused cell back from the table view, it already hass this label in it. What you can do to avoid adding duplicate labels is add it only if you need to allocate a new cell. However, this can make retrieving the label back from a reused cell quite complicated.
I would personally suggest creating a custom UITableViewCell in interface builder, and creating a custom UITableViewCell subclass that has a UILabel property.
I have a big problem with an UITableView, I want to use a label inside the cell, so I use this method for do it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
}
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
return cell;
}
in this way I can see all the data on my table, but the label are overlap, than I try to use this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
}
else {
}
return cell;
}
the label are OK but in this case I can see on my table only 5 data, and this 5 data are repeat for some time...
For example if in the first case on my table I can see: 1,2,3,4,5,6,7,8,9,10,...
in the second case I seee: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...
where is the problem?
add this code
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
before
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
Currently you add a label to the cell and the next time the cell is reused..the label is still there and you add a label on top of it.
The code you posted is specifying UITableViewCellStyleSubtitle as the cell style, which means each cell will a text label and detail text label available in its corresponding properties textLabel, and detailTextLabel. So there's no reason for you to allocate additional instances of UILabel. Instead, just populate the text properties of the existing labels. For example, you could rewrite your implementation like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
cell.textLabel.text = (indexPath.section == 0 ?
[array objectAtIndex:indexPath.row] :
#"ReadMore");
return cell;
}
I have a tableview cell in a view which has some text in it,the verses of bible.but the scrolling is not at all smoother i have this code for UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [delegate.allSelectedVerseEnglish count];
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier
] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
//pinch for serchpage
UIPinchGestureRecognizer *longPressRecognizer =
[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetected:)];
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
//longtap for simpklepopupview
UILongPressGestureRecognizer *longPressgesture =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetectedgesture:)];
[self.view addGestureRecognizer:longPressgesture];
[longPressgesture release];
if (imagedarkbackground.hidden == NO) {
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
this is the cell height code
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize textSizelabel = [[NSString stringWithFormat:#"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height +18;
return textSizelabel.height +18;
is there any mistake in my code that cause slow and sluggish scrolling.
Thanks in advance.
Following code has few fixes:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [delegate.allSelectedVerseEnglish count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"readCell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
if (imagedarkbackground.hidden == NO)
{
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
}
//** If there is only one table then move the following case also inside the cell== nil clause with few minor changes....**/
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
Move out following code in more suitable method, as it has nothing to do with cell or tableview.
Also, review your code in if(tableView = table). If there is only one table then move the code where it fits.
//pinch for serchpage
UIPinchGestureRecognizer *longPressRecognizer =
[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetected:)];
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
//longtap for simpklepopupview
UILongPressGestureRecognizer *longPressgesture =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetectedgesture:)];
[self.view addGestureRecognizer:longPressgesture];
[longPressgesture release];
I am not sure what you want to do in heightForRow method as it returns two values which is not possible.
Update
If you have two tableviews then you can use following code, as both tableviews will have separate cells that they will use so we need not set/reset other properties.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"readCell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
if (imagedarkbackground.hidden == NO)
{
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
if(tableView == table1)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
else if (tableView == table2)
{
// do something if needed... else leave it
}
}
if(tableView == table1)
{
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
}
else if(tableView == table2)
{
// set text or changeable properties here... for table 2
}
return cell;
}
heightForRowAtIndexPath:
For two tables use following code(with your changes):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
if (tableView = table1)
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
height = textSize.height +18;
}
else if (tableView = table1)
{
CGSize textSizelabel = [[NSString stringWithFormat:#"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
height = textSizelabel.height +18;
}
return height;
}
Thanks,
I need to do this: when a user select a row from an tableview to send the text from that row to a textfield from an other tableview.
For example, if I select Services from
I want to see Services here,near Type de Partenaire :
I tried this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
self.recherchePartenaireTableView.partenaire.text=cellText;
}
and when Button ok is pressed :
-(IBAction)OkButtonPressed:(id)sender{
NSLog(#"BTN Ok");
[self.recherchePartenaireTableView.tableviewrecherchepartenaire reloadData];
[self.navigationController popViewControllerAnimated:YES];
}
but this is not working. Can anyone help me? Thanks in advance..
in implementation file for first image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
}
// Set up the cell...
[[cell textLabel] setText: [typePartenaireArray objectAtIndex:indexPath.row]] ;
return cell;
}
** in implementation file for second image**
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row==0)
{
partenaire=[[UITextField alloc] init];
partenaire.frame = CGRectMake(200,10,80,50);
partenaire.textColor=[UIColor grayColor];
partenaire.text=#"Tous";
[partenaire setKeyboardAppearance:NO];
[cell.contentView addSubview:partenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [arraytableview objectAtIndex:indexPath.row]] ;
return cell;
}
Override the init method of the second UITableView to accept an extra argument (in your case the text of the cell selected in the current UITableView). And in the second UITableView where you configure the cells, set their text as you wish with this parameter you just received from the previous tableView which the second tableview object was alloc initted.
You have to insert your text into your array arraytableview in position 1 (arrays start at 0). Make sure it is declared as NSMutableArray otherwise you can't change it.
So you have to do: [arraytableview insertObject: cellText atIndex: 1] in your OK method. For this you might have to introduce a variable that holds the currently selected text to "transfer" it from the selected method to the OK method.
Or you just add it in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, but then it would add the text immediately without pressing OK. really depends what you want
I hope it's clear now.
Code to define UITableViewCell with multiple lines, textfields, etc:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview: imageView];
titleLabel = [[UILabel alloc] initWithFrame: CGRectZero];
[titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setHighlightedTextColor:[UIColor darkGrayColor]];
[titleLabel setLineBreakMode: UILineBreakModeWordWrap];
titleLabel.numberOfLines = 2;
[self.contentView addSubview: titleLabel];
description = [[UITextField alloc] initWithFrame: CGRectZero];
[description setFont:[UIFont systemFontOfSize:12.0]];
[description setTextColor:[UIColor darkGrayColor]];
[description setHighlightedTextColor:[UIColor darkGrayColor]];
[self.contentView addSubview: description];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
// D_IN;
[imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
[titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines
[description setFrame:CGRectMake(40.0, 37.0, 250.0, 3.0)];//not used
// D_OUT;
}
Make sure that the layout is correct when specifying the starting x and y for each new component otherwise you won't see anything!!!
Then just set it with
titleLabel.text =
or
description.text =