I implemented the following method in my tableviewcontroller for showing favorites with empty star and filled star:
-(IBAction) tapped:(UIButton *)sender
{
if([sender isSelected])
{
//...
[sender setImage:[UIImage imageNamed:#"fav-empty.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
//...
[sender setImage:[UIImage imageNamed:#"fav-filled.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
NSLog(#"%#",sender);
}
}
and I tried to use in this part:
- (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];
UILabel *recept_neve=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
[recept_neve setTag:1];
UIButton *kedvenc_btn=[UIButton buttonWithType:UIButtonTypeCustom];
[kedvenc_btn setImage:[UIImage imageNamed:#"fav-empty.png"] forState:UIControlStateNormal];//default appereance on the startup
kedvenc_btn.frame=CGRectMake(200, 20, 50, 50);
[kedvenc_btn setTag:2];
[kedvenc_btn addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
[[cell contentView] addSubview:recept_neve];
[[cell contentView] addSubview:kedvenc_btn];
//[(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];
//[(UIButton *) [cell contentView] viewWithTag:2];
}
// Configure the cell...
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
[(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];
return cell;
}
My problem is that every fifth button in the cells are working as would be the same.
If I tap on the button(empty star) in the first cell, the result will be the following: the first button becoming filled-star, so far so good(the expected behaviour), but in the same time the sixth star becoming filled too. Or if I tap on the second one, the seventh star getting filled and vica versa...
I logged the buttons where I tapping , and I realised, that the memory adresses are equals in the case of buttons above mentioned.
Sorry about my english, and thanks a lot for every help in advance!
the problem was that you reused the buttons all the time.
I show you a sample working code to structure a customized UITableViewCell correctly...
Let's do it step by step.
Create a new class called MyObjTableViewCellForSelection and provide these methods. Adapt it to your needs!
#class MyObj;
#interface MyObjTableViewCellForSelection : UITableViewCell
{
MyObj *myObj;
UIImageView *imageView;
UILabel *titleLabel;
}
#property (nonatomic,strong) MyObj *myObj;
#property (nonatomic,strong) UIImageView *imageView;
#property (nonatomic,strong) UILabel *titleLabel;
#end
Then the implementation:
#implementation MyObjTableViewCellForSelection
#synthesize myObj;
#synthesize imageView;
#synthesize titleLabel;
- (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];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
[imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
[titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines
}
- (void)setMyObj:(MyObj *)myObjX {
if (myObjX != myObj) {
[myObj release];
[myObjX retain];
//myObj = myObjX; //only with ARC on
}
titleLabel.text = whatever you need accessing myObj
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// selection has to be adapted to your structure
[super setSelected:selected animated:animated];
switch (myObj.selectedFlag) {
case MYOBJ_STATUS_UNSELECTED:
imageView.image = [UIImage imageNamed:#"EmptyBullet.png"];
break;
case MYOBJ_STATUS_SELECTED:
imageView.image = [UIImage imageNamed:#"GreyBullet.png"];
break;
default:
break;
}
#end
now in your view class you can finally do:
- (MyObj *)myObjForIndexPath:(NSIndexPath *)indexPath {
MyObj* tmpObj = get the corresponding object
return tmpObj;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyObjTableViewCellForSelection *cell = (MyObjTableViewCellForSelection *)[tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (cell == Nil) {
cell = [[ECUTableViewCellForSelection alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellID"];
}
cell.myObj = [self myObjForIndexPath:indexPath]; //the assignment calls setMyObj of CLASS ECUTableViewCellForSelection that you just declared
return cell;
}
So finally that's the clean way of doing it...
Related
I am new in iOS development, I have a custom table view on which have placed 3 buttons namely,
Add comment
Up arrow
Down arrow
I want to increase the height of cell when down arrow button is pressed, also make add comment button, downarrow button visible.
My code,
#import "JCoutGoingConfess.h"
#interface JCoutGoingConfess (private)
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
#end
#implementation JCoutGoingConfess
#define kCellHeight 132.0
#synthesize outConfessionNavbarBAckBtn,JCoutGoingTbl;
//---------------------------- TableView---------------------------------------
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
{
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:1 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return kCellHeight * 2.0;
}
return kCellHeight;
}
- (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];
}
// ----------------------- cellbackground image-----------------------------
if([self cellIsSelected:indexPath]){
cell.backgroundView =[[UIImageView alloc]initWithFrame:CGRectMake(160, 151, 320, 108)];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"inCellFullBg.png"]];
}
// ******** imageView on cell *******
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(112,0, 90, 90)];
imgView.image = [UIImage imageNamed:#"friendsfacepic.png"];
[cell.contentView addSubview:imgView];
//----------------------------Cell buttons-------------------------------------------
if (JcOutCellSize==152)
{
UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom];
[AddComment addTarget:self
action:#selector(TestBtns:)
forControlEvents:UIControlEventTouchDown];
AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);
[cell.contentView addSubview:AddComment];
UIImage *buttonAddComment = [UIImage imageNamed:#"addcomment.png"];
[AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];
[cell.contentView addSubview:AddComment];
UIButton *UpArrow = [UIButton buttonWithType:UIButtonTypeCustom];
[UpArrow addTarget:self
action:#selector(ResizeCell:)
forControlEvents:UIControlEventTouchDown];
//[DownArrow setTitle:#"Arrow" forState:UIControlStateNormal];
UpArrow.frame = CGRectMake(143.0, 136.0, 26.0, 16.0);
[cell.contentView addSubview:UpArrow];
UIImage *buttonUp = [UIImage imageNamed:#"upArrow.png"];
[UpArrow setBackgroundImage:buttonUp forState:UIControlStateNormal];
[cell.contentView addSubview:UpArrow];
}
//----------------------------------------------------------------------------------
if (JcOutCellSize==132)
{
NSLog(#" down arrow %f",JcOutCellSize);
UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeCustom];
[DownArrow addTarget:self
action:#selector(IncreseCellHight:)
forControlEvents:UIControlEventTouchDown];
DownArrow.frame = CGRectMake(143.0, 116.0, 26.0, 16.0);
[cell.contentView addSubview:DownArrow];
UIImage *buttondown = [UIImage imageNamed:#"upDownArrow.png"];
[DownArrow setBackgroundImage:buttondown forState:UIControlStateNormal];
[cell.contentView addSubview:DownArrow];
NSLog(#" cell size = %f",JcOutCellSize);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
[JCoutGoingTbl beginUpdates];
[JCoutGoingTbl endUpdates];
}
- (void)viewDidLoad
{
selectedIndexes = [[NSMutableDictionary alloc] init];
[super viewDidLoad];
}
- (void)viewDidUnload
{
selectedIndexes = nil;
[super viewDidUnload];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
This is not working as expected, what could be the problem?
set any integer variable value in button action and put condition in heightForRowAtIndexPath, whatever size you want, and then reload table
I need help with this. I have updated xcode to the latest and when I try to get test, the old code throws an error "class is not key value coding-compliant for the key scrollView."
Funny thing is that there is no scrollview in this. I have other views almost exact code and it works fine. Here is the code. there is no nib for this either so the taking out the scrollview from the view won't work.
#import "pearGalleryController.h"
#import "pearGallery.h"
#implementation pearGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:100];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.navigationItem.title = #"Variety of Pears";
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 122;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell is an object of the UITableViewCell this assign an object to a cell 2 memory areas, o need to auto release on the alloc bcause it is an object
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Set up the cell...
int picIndex = [indexPath row]*3;
UIButton* tempView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex]]
forState:UIControlStateNormal];
tempView.tag = picIndex;
[tempView addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
tempView = [[UIButton alloc]initWithFrame:CGRectMake(106, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex+1]]
forState:UIControlStateNormal];
tempView.tag = picIndex+1;
[tempView addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
tempView = [[UIButton alloc]initWithFrame:CGRectMake(212, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex+2]]
forState:UIControlStateNormal];
tempView.tag = picIndex+2;
[tempView addTarget:self action:#selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
return cell;
}
-(void)buttonClick:(id)sender
{
UIButton* btn = (UIButton*)sender;
int index = btn.tag;
pearGallery *anotherViewController = [[pearGallery alloc] initWithNibName:#"pearGallery" bundle:[NSBundle mainBundle]];
anotherViewController.myIndex = index;
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
//UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"video.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[super dealloc];
}
#end
and the h file
#import
#interface pearGalleryController : UITableViewController {
}
#end
probably a dangling reference in your xib file, check to make sure references are cleared.
Usually this error occurs when you have an object , in your case it is the 'scrollView' which is attached via the interface builder to the scrollView item in storyboard/xib.
Check your links in interface builder ( furthest tab to the right ) and that you don't have a duplicate link to another IBOutlet.
I find this only occurs to me when I link an object up then 'mistype' my link , I go to change it but don't remove the old link from interface builder ie SCrollview and scroLLview2
Go to your interface builder ( connection tab ) and check you don't have duplicate links.
Remove all links to your 'scrollview' and 're link' to your .h file then try again
you may have typed scrollView then changed it to scrollview and not realised..good luck
In my application i am trying to adjust the position of the textlabel and detailtextlabel in a tableview cell.I am doing like this `
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
}
cell.textLabel.frame = CGRectMake(0,0, 150, 40);
cell.detailTextLabel.frame = CGRectMake(155, 55, 150, 40);
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text =#"name";
cell.detailTextLabel.text=#" status";
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//cell.detailTextLabel.textAlignment = UITextAlignmenteft;
//cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;
}
`but seems to be having no effect.can anybody help me to point me where i am going wrong?
textLabel and detailTextLabel are readonly so you can not change them.
#property(nonatomic, readonly, retain) UILabel *textLabel
#property(nonatomic, readonly, retain) UILabel *detailTextLabel
See here
What I would suggest is that you could create your custom UITableViewCell instead, you will have more control over it's content and can add custom subviews in there as well.
Here are some tutorials for that:
From IB
From Code
you need to override layoutSubviews method
Create a customcell class and inherit with uitableviewcell
#try this code
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews{
//Set here your frame
self.textLabel.frame = CGRectMake(0, , 320, 30);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
I want to make a UITableView, which has many columns, so the whole cell can't display the all data, and the first column shows titles, this row can't move flatly, but other rows can move flatly.
I tried adding UITableView to UIScrollView, but failed
Is there any ways to achieve this requirement?
You need to make a custom TableViewCell to achieve this properly.
EDIT:
A quick example:
#import <UIKit/UIKit.h>
#interface MultiColumnTableViewCell : UITableViewCell
{
UIButton *leftButton;
UIButton *middleButton;
UIButton *rightButton;
}
#property (nonatomic,retain) UIButton *leftButton;
#property (nonatomic,retain) UIButton *middleButton;
#property (nonatomic,retain) UIButton *rightButton;
#end
#import "MultiColumnTableViewCell.h"
#implementation MultiColumnTableViewCell
#synthesize leftButton;
#synthesize middleButton;
#synthesize rightButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
leftButton = [[UIButton alloc] init];
[leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
leftButton.titleLabel.textAlignment = UITextAlignmentCenter;
leftButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
leftButton.titleLabel.numberOfLines = 2;
middleButton = [[UIButton alloc] init];
[middleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
middleButton.titleLabel.textAlignment = UITextAlignmentCenter;
middleButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
middleButton.titleLabel.numberOfLines = 2;
rightButton = [[UIButton alloc] init];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
rightButton.titleLabel.textAlignment = UITextAlignmentCenter;
rightButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
rightButton.titleLabel.numberOfLines = 2;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Quick usage in tableview:
- (MultiColumnTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MultiColumnTableViewCell *cell = (MultiColumnTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MultiColumnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set up the cell...
float tableviewWidth = self.myTableView.frame.size.width/3;
int irow = indexPath.row;
NSString *cellValue = myString;
cell.leftButton.frame = CGRectMake(0, 0, tableviewWidth, 64.0);
cell.leftButton.tag = someTag;
[cell.leftButton setTitle:cellValue forState:UIControlStateNormal];
[cell.leftButton addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.leftButton];
cellValue = someOtherString;
cell.middleButton.frame = CGRectMake(tableviewWidth, 0, tableviewWidth, 64.0);
cell.middleButton.tag = someOtherTag;
[cell.middleButton setTitle:cellValue forState:UIControlStateNormal];
[cell.middleButton addTarget:self action:#selector(someOtherAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.middleButton];
cellValue = yetAnotherString
cell.rightButton.frame = CGRectMake(tableviewWidth*2, 0, tableviewWidth, 64.0);
cell.rightButton.tag = yetAnotherTag;
[cell.rightButton setTitle:cellValue forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:#selector(yetAnotherAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:cell.rightButton];
return cell;
}
I used an UINavigationController pushViewController to present an ViewController, on the view of the viewcontroller, there is an UITablView.
I used the codes below to draw UILabel on UITableViewCell, but it does not display, only when I tap the row where the UIlabel is, it will appear, if I tap another row, it disappear again.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier1 = #"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
if (cell == nil)
{
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier1] autorelease];
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
UISwitch *switchview=[[UISwitch alloc]init];
switchview.tag=11106 ;
cell.accessoryView= switchview;
//switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
//[cell.contentView addSubview:switchview];
[switchview addTarget:self action:#selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[switchview release];
break;
}
case _PSMultiValueSpecifier:
{
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
//draw a label, but actually it does display,
UILabel *labelCell =[ [UILabel alloc]init];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:18.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
[labelCell release];
break;
}
}
}
cell.text=#"test";
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
cell.accessoryType=UITableViewCellAccessoryNone;//
UISwitch *tem=(UISwitch*) [cell.contentView viewWithTag:11106];//cell.accessoryView;//
tem.on= true;
break;
}
case _PSMultiValueSpecifier:
{
UILabel *newLabelCell=(UILabel*)[cell.contentView viewWithTag: 11101];//intV];
newLabelCell.text=#"Items";//it should display, but actually not, only when I tap the row "didselect" the text display
break;
}
};
return cell;
}
In codes above UISwitch are displayed, but if I use codes
switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[switchview addTarget:self action:#selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchview];
to replace cell.accessoryView= switchview;
UISwitch will be the same thing as UILabel
Welcome any comment
Thanks
interdev
Try using initWithStyle to create your UITableViewCell...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
Note: initWithFrame deprecated, and you're using a CGRectZero for the frame