I need to use two tableview in a view. Also i need to create these tables with custom cells.
I tried something but i could not see even simple string in my table's cell.
What's wrong here?
Here is my updated code;
I have still problems, i am taking invalid argument exception [LastVisitOrder superview]: unrecognized selector sent to instance .Since i have changed uiview to uiview controller.
- (id)initWithFrame:(CGRect)frame
{
self.view = [super initWithFrame:frame];
if (self) {
self.view.frame = CGRectMake(0, 0, 1024, 768);
[self.view setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
UIView *popup = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-860)/2,(self.view.frame.size.height-570)/2, 860, 570)];
[popup setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"popup_bckgrnd"]]];
[self.view addSubview:popup];
UILabel *customerName = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 300, 30)];
[customerName setBackgroundColor:[UIColor clearColor]];
[customerName setText:#"Beşiktaş Eczanesi"];
[customerName setFont:[UIFont fontWithName:#"Arial-BoldMT" size:18]];
[popup addSubview:customerName];
UILabel *customerDetail = [[UILabel alloc]initWithFrame:CGRectMake(50, 70, 300, 30)];
[customerDetail setBackgroundColor:[UIColor clearColor]];
[customerDetail setText:#"Tel: 231 213 23 23 Bla bla cad. Bla bla Sok. Bla bla Apt. No:Bla Blastan/Bistanbul"];
[customerDetail setFont:[UIFont fontWithName:#"Arial" size:14]];
[popup addSubview:customerDetail];
UIButton *close = [[UIButton alloc]initWithFrame:CGRectMake(780, 65, 19, 19)];
[close setBackgroundImage:[UIImage imageNamed:#"kapat"] forState:UIControlStateNormal];
[close addTarget:self action:#selector(closePopUp:) forControlEvents:UIControlEventTouchUpInside];
[popup addSubview:close];
UIView *titleLine = [[UIView alloc]initWithFrame:CGRectMake(35, 100, 790, 2)];
[titleLine setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"baslik_cizgi"]]];
[popup addSubview:titleLine];
dateTable = [[UITableView alloc]initWithFrame:CGRectMake(48.5, 165, 200, 345)];
dateTable.layer.cornerRadius = 10;
[dateTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:dateTable];
dateTable.delegate=self;
dateTable.dataSource=self;
UIImageView *visitDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(70, 122, 148, 30)];
[visitDatesLabel setImage:[UIImage imageNamed:#"en_son_ziyaret"]];
[popup addSubview:visitDatesLabel];
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(49.5, 160, 200, 2)];
[line setImage:[UIImage imageNamed:#"ust_cizgi"]];
[popup addSubview:line];
UIImageView *line2 = [[UIImageView alloc]initWithFrame:CGRectMake(270.5, 160, 540, 2)];
[line2 setImage:[UIImage imageNamed:#"ust_cizgi"]];
[popup addSubview:line2];
UIImageView *productsDatesLabel = [[UIImageView alloc]initWithFrame:CGRectMake(300, 122, 102, 29)];
[productsDatesLabel setImage:[UIImage imageNamed:#"urunler"]];
[popup addSubview:productsDatesLabel];
UIImageView *verticalLine = [[UIImageView alloc]initWithFrame:CGRectMake(430, 115, 2, 45)];
[verticalLine setImage:[UIImage imageNamed:#"dikey_cizgi"]];
[popup addSubview:verticalLine];
UILabel *firstMonth = [[UILabel alloc]initWithFrame:CGRectMake(455, 126, 148, 30)];
[firstMonth setBackgroundColor:[UIColor clearColor]];
[firstMonth setText:#"OCAK"];
[firstMonth setFont:[UIFont fontWithName:#"Arial-BoldMT" size:12]];
[firstMonth setTextColor:[UIColor grayColor]];
[popup addSubview:firstMonth];
UILabel *secondMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75, 126, 148, 30)];
[secondMonth setBackgroundColor:[UIColor clearColor]];
[secondMonth setText:#"ŞUBAT"];
[secondMonth setFont:[UIFont fontWithName:#"Arial-BoldMT" size:12]];
[secondMonth setTextColor:[UIColor grayColor]];
[popup addSubview:secondMonth];
UILabel *thirdMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75, 126, 148, 30)];
[thirdMonth setBackgroundColor:[UIColor clearColor]];
[thirdMonth setText:#"MART"];
[thirdMonth setFont:[UIFont fontWithName:#"Arial-BoldMT" size:12]];
[thirdMonth setTextColor:[UIColor grayColor]];
[popup addSubview:thirdMonth];
UILabel *rangeMonth = [[UILabel alloc]initWithFrame:CGRectMake(455+75+75+100, 126, 148, 30)];
[rangeMonth setBackgroundColor:[UIColor clearColor]];
[rangeMonth setText:#"OCAK-MART"];
[rangeMonth setFont:[UIFont fontWithName:#"Arial-BoldMT" size:12]];
[rangeMonth setTextColor:[UIColor grayColor]];
[popup addSubview:rangeMonth];
UIImageView *verticalLine2 = [[UIImageView alloc]initWithFrame:CGRectMake(510, 115, 2, 45)];
[verticalLine2 setImage:[UIImage imageNamed:#"dikey_cizgi"]];
[popup addSubview:verticalLine2];
UIImageView *verticalLine3 = [[UIImageView alloc]initWithFrame:CGRectMake(590, 115, 2, 45)];
[verticalLine3 setImage:[UIImage imageNamed:#"dikey_cizgi"]];
[popup addSubview:verticalLine3];
UIImageView *verticalLine4 = [[UIImageView alloc]initWithFrame:CGRectMake(670, 115, 2, 45)];
[verticalLine4 setImage:[UIImage imageNamed:#"dikey_cizgi"]];
[popup addSubview:verticalLine4];
medSalesTable = [[UITableView alloc]initWithFrame:CGRectMake(270.5, 165, 540, 345)];
medSalesTable.layer.cornerRadius = 10;
[medSalesTable setBackgroundColor:[UIColor clearColor]];
[popup addSubview:medSalesTable];
medSalesTable.delegate=self;
medSalesTable.dataSource=self;
}
return self;
}
-(void) closePopUp:(id)sender{
[self.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath");
if (tableView == dateTable)
{
static NSString *CellIdentifier = #"VisitDateCell";
VisitDateCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[VisitDateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.lastVisit setText:#"fucker"];
return cell;
}
else if(tableView == medSalesTable){
static NSString *CellIdentifier2 = #"OrdersCell";
OrdersCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[OrdersCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
[cell.textLabel setText:#"fucker"];
return cell;
}
}
#end
In your datasource methods you need to distinguish between the two tables.
//cellForRowAtIndexPath
if (tableView == medSalesTable) {
// configure the first table
}
else {
// configure the second table
// use a different cell identifier
}
Make sure you do this also in numberOfRowsInSection, and numberOfSectionsInTableView if necessary. This assumes that you set both table views datasource property to the controller.
Use cell = [[[NSBundle mainBundle] loadNibNamed:#"VisitDateCell" owner:self options:nil] objectAtIndex:0];
Instead of cell = [[[VisitDateCell alloc] initWithFrame:CGRectZero] autorelease];
Then use cell.textLabel.text = #"YOUR_TEXT";
UPDATE!
For having access to your variable date you need to create it global in VisitDateCell.h.Like:
#property (nonatomic, strong) UILabel *date;
Then it your TableView to achieve the access use:
(VisitDateCell*)cell.date = #"YOUR TEXT";
You don't need in your custom method setText. Just use this code in cellForRows.
Hope it will help!
This is an elegant way to do that I use all the time..
Here's only working on create multiples custom cells, there aren't any code for calculate the height of the cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
Call a function to create all custom cells.
Send the tableview and the indexPath to this function.
So, your code will be clean and easy to read an maintenance =D
DON'T forget to change the height of each cell
*/
if (indexPath.row < 3)
return [self createACustomCell1:tableView indexPath:indexPath];
else
return [self createACustomCell2:tableView indexPath:indexPath];
}
//*************
// Create CUSTOM CELL 2
//*************
-(UITableViewCell *)createACustomCell1:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
static NSString *CUSTOMCELL_1 = #"CUSTOMCELL_1";
CustomCell_1 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
if (!cell){
[anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_1
bundle:nil] forCellReuseIdentifier:CUSTOMCELL_1];
cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1];
}
// Cell customization above
return cell;
}
//*************
// Create CUSTOM CELL 2
//*************
-(UITableViewCell *)createACustomCell2:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{
static NSString *CUSTOMCELL_2 = #"CUSTOMCELL_2";
CustomCell_2 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
if (!cell){
[anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_2
bundle:nil] forCellReuseIdentifier:CUSTOMCELL_2];
cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2];
}
// Cell customization above
return cell;
}
Related
I have a UITableView with paging enabled. At first I can scroll very smoothly between cells. However, after scrolling back and forth a few times, it becomes rather choppy. Am I not creating/reusing my cells properly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
return cell;
}
If you want to reuse them then make sure you add the content to your UITableViewCell only once and on reusing it just update it (e.g. update the image and the price accordingly).
Otherwise, what you get with your implementation is adding subviews price and image everytime (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath gets called. Just try debugging the number of subviews your cell has to see if I am right.
You could go this way:
if ( ! [cell viewWithTag: 1])
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
image.tag = 1;
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
}
if ( ! [cell viewWithTag: 2])
{
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
priceLabel.tag = 2;
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
}
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
UIImage *image = [cell viewWithTag: 1];
[image setImage:[UIImage imageWithData:purch.image]];
UILabel *priceLabel = [cell viewWithTag: 2];
[priceLabel setText:#"$10.0"];
This is because - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is being called each time the cell comes into the view. Add the images to an array (in viewDidLoad or somewhere), and you'll get a smoother result.
You are not reusing the cells, which causes the lag/choppy scrolling.
suggestion:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
image.tag = 10;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell.contentView addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
price.tag = 11;
[cell.contentView addSubview:price];
}
//update the value here
UIImageView *imgV = (UIImageView *)[self.contentView viewWithTag:10];
// update the imageView here
//imgV.image = image
UIView *vv = (UIView *)[self.contentView viewWithTag:11];
// Update the UIView here
//vv.text = #"text here";
//Put setselection style here to allow the cell to be loaded before setting the property value
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I have iphone app in which i am adding tableView and inside tableView cell i am adding the textfield but problem is that it shows line below the textfiled in tableView as shown in the image attached here is the my code.
I want to remove the line which is coming below the input filed Enter Tag Here
-(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];
}
}
tableView.backgroundView=nil;
if(indexPath.section==0){
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,1,249,28)];
tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
tagInputField.textAlignment=UITextAlignmentLeft;
tagInputField.backgroundColor=[UIColor whiteColor];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#"Enter tag here "];
tagInputField.textColor =[UIColor grayColor];
[cell addSubview:tagInputField];
if(tagArray.count >0)
{
[tagInputField setBorderStyle:UITextBorderStyleNone];
}
else {
[tagInputField setBorderStyle:UITextBorderStyleRoundedRect];
}
return cell;
}
if(indexPath.section==1) {
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 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 fontWithName:#"Myriad-Pro" size:8];
cell.textLabel.textColor =[UIColor grayColor];
cell.backgroundColor=[UIColor whiteColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
[tagInputField setFrame:CGRectMake(8,1,240,30)];
tableView.backgroundColor=[UIColor whiteColor];
[publishButton setFrame:CGRectMake(40,560,250, 50)];
[descriptionTextImageView setFrame:CGRectMake(48,450,250,90)];
return cell;
}
Update:
1) set property of tableview separatorStyle as UITableViewCellSeparatorStyleNone.
2) set property of SeparatorColor like:
[self.tableView setSeparatorColor:[UIColor clearColor]];
3) other simple way to remove seprator is add custom seprator to simple UIView of 1px height:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // as per your requirement set color.
[cell.contentView addSubview:separatorLineView];
4) write this delegate method of tableview:
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;// here remove your footer
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
5) try this one:
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
It can be from xib. Set separator to None, as below screen shot show. Hope it will help. Thanks
You should set separatorStyle property of UITableView to UITableViewCellSeparatorStyleNone.
I'm trying to change the text color of a label in the cell when Current Date == Facebook Birthday List Date.
In cellForRowAtIndexPath: id did this
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
This works to change the color but when I scroll it changes back to the old color. How can I fix this?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableidentifier=#"SimpleTableItem";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[[cell contentView] subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
contentview = [[[UIView alloc]init] autorelease];
[contentview setFrame:CGRectMake(0, 0,320,80)];
//Display Friends Name
nameLabel = [[[UILabel alloc] init]autorelease];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
[nameLabel setFont:[UIFont fontWithName:#"Politica" size:20]];
[nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:#"name"]];
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
[contentview addSubview:nameLabel];
[cell.contentView addSubview:contentview];
return cell;
[birthdaylist reloadData];
}
Because tableview reuses it's cells while scrolling, the issue mostly is because you are writing the logic above inside if(!cell)condition.
To rule out potential problems with data try replacing
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
with
if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 15)
Do you always see the colour of rows 0, 1 and 15 set properly?
Could it be that totalbirthlistArray changes unintentionally between scrolls, say in [birthdaylist reloadData] call?
replace your cellForRowAtIndexPath with this.I think, this will solves your issue.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableidentifier=#"SimpleTableItem";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableidentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableidentifier]autorelease];
contentview = [[[UIView alloc]init] autorelease];
[contentview setFrame:CGRectMake(0, 0,320,80)];
//Display Friends Name
nameLabel = [[[UILabel alloc] init]autorelease];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setFrame:CGRectMake(76, 5, 200, 45)];
nameLabel.tag = 1;
[nameLabel setFont:[UIFont fontWithName:#"Politica" size:20]];
[nameLabel setText:[[[monthdataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:#"name"]];
[contentview addSubview:nameLabel];
[cell.contentView addSubview:contentview];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *nameLabel = [cell.contentView viewWihtTag:1];
if([curdatemonthstring isEqualToString:[[totalbirthlistArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]])
{
[nameLabel setTextColor:[UIColor purpleColor]];
}
return cell;
[birthdaylist reloadData]; //I don't what is the use of this.
}
I'm a newbie. I am using this code to create a UITableViewCell but when I reload the table the button's image is not always correct, although all labels work fine. I don't know why. How can I fix this issue?
- (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];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.tag = 1000;
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 25)];
UploadTimeLabel.tag = 2000;
UploadTimeLabel.backgroundColor = [UIColor clearColor];
UploadTimeLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
UploadTimeLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview: UploadTimeLabel];
[UploadTimeLabel release];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
market = [[UIButton alloc] init];;
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
}
if( [temp count] > 0)
{
UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000];
fileNameLbl.text =[temp objectAtIndex:indexPath.row];
UILabel *uploadlbl = (UILabel*)[cell.contentView viewWithTag:2000];
uploadlbl.text =[UploadTimeAllArr objectAtIndex:indexPath.row];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
return cell;
}
Your main problem here is that you are recreating new views in your cell every time this method is called. You want to create all reusable elements inside the if(cell == nil) otherwise it will make duplicates. Anything that is dynamic must be created outside of this. I took your code and modified it. This should work better.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Everything that does not change should go in here!
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell addSubview:pricelabel];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:market];
}
// find market button, since we could be reusing a cell we cannot rely on a tag
// value to find it. (This would only work with one button though).
UIButton *market;
for (UIView *subview in cell.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
market = (UIButton *)subview;
break;
}
}
// set all defaults in case of reuse
[market setImage:[UIImage imageNamed:#"DefaultImage.png"] forState:UIControlStateNormal];
market.selected = YES;
market.enabled = NO;
market.clearsContextBeforeDrawing = NO;
if([sellingArray count] > 0) {
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) {
// not sure if this is supposed to be YES or NO
market.clearsContextBeforeDrawing = YES;
[market setSelected:NO];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
market.enabled = YES;
}
}
[market setTag:indexPath.row];
return cell;
}
Since it appears you are not using ARC, make sure you look over this code for any needed reference counting.
dequeReusablecellWithIdentifier: method get return the cell instance already created available,If the reference points still to nil ,we need a valid cell and create one cell to return from that cellForRowatIndexpath: method.That is what being checked in the (cell ==nil).When you create a new cell it is creation and hence all settings custom and all has to be done here.
Second Edit:
This was copied from an answer above:
inside the -cellForRowAtIndexPath: method:
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSubtitle];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
//Add all your UILabel INITIATION stuff here as well
}
UIButton *marketButton;
for (UIView *subview in cell.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
marketButton = (UIButton *)subview;
break;
}
}
marketButton.tag = [indexPath row];
UILabel *priceLabel = [cell.contentView viewWithTag:3000];
UILabel *uploadTimeLabel = [cell.contentView viewWithTag:2000];
//Set up your labels and button now
return cell;
}
EDIT: Leaving my original answer below for posterity but I see that you are setting the table index row as the MarketButton's tag. If you're using that to figure out which dataSource object to query, this is bad practice. You should be making a custom cell which can hold a reference to the object in your data source, so you don't have to ask the button for its tag, and then ask the data source array for the object at index:tag.
The reason this is bad is because somewhere, the state of your array could change, but the table cell is still displayed and still holds a tag pointing at the wrong index. If you just have the cell keep track of the object in question, no matter what happens to the array structure you're guaranteed to be modifying the object you need to.
The only thing I would change about Firo's answer is to just add a "tag" property to each view in the cell, so you don't have to iterate each time you want to find it.
Also took out the [[UIButton alloc]init] line because it's superfluous and might be considered a dangling pointer.
if (cell == nil) {
// Everything that does not change should go in here!
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
market.tag = 9999;
[cell.contentView addSubview:market];
}
//don't have to do UIView iteration here
UIButton *marketButton = [cell.contentView viewWithTag:9999];
Hi am using 2 UIImageView in a cellrow with tag, each row is displayed sequentially by populating images from an array like (0th image,1st image) for row 0, and (2nd image,3rd image) for row 1 and so on...
i am successful in displaying the image with appropriate tag for each image, but am unsuccessful in getting the tag for image or cell in didrowselect method...i need the tag number to directly pass the tagnumber as array index to detailsegue...Am new to IOS programming, i would like to know if am doing anything wrong here, please suggest a way to get the appropriate tag number...your help in this regard is greatly appreciated, as am struck with only this task to be completed!!!
Pasted my code for your reference: self.tagnumber=0; this code is for displaying even number of images.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Check Even Rows or odd rows
if(_objects.count%2==0)
{
return (_objects.count/2);
}
else
{
return ((_objects.count/2)+1);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyCellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
}
NSInteger itemcount = _objects.count;
NSLog(#"RowCount:%d",_objects.count/2);
if(itemcount%2==0)
{
NSLog(#"Image Count is Even!");
if(itemcount!=self.tagnumber)
{
RSSItem *object = _objects[self.tagnumber];
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 145, 95)];
imageView1.tag = self.tagnumber;//1;
NSLog(#"Tag number for Image1:%d", self.tagnumber);
imageView1.image = [UIImage imageWithData:object.artimageData];
[imageView1 setContentMode:UIViewContentModeScaleAspectFill];
[imageView1 setClipsToBounds:YES];
[cell.contentView addSubview:imageView1];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 145, 30)];
label1.tag = self.tagnumber;
[label1 setFont:[UIFont fontWithName:#"AppleGothic" size:14.0]];
[label1 setTextColor:[UIColor blackColor]];
[label1 setContentMode:UIViewContentModeScaleToFill];
[label1 setTextAlignment:NSTextAlignmentCenter];
label1.text = #"Added 2days ago";;
[label1 setNumberOfLines:1];
[cell.contentView addSubview:label1];
//Display next Image
self.tagnumber++;
object = _objects[self.tagnumber];
UIImageView *imageView2= [[UIImageView alloc]initWithFrame:CGRectMake(165, 4, 145, 95)];
imageView2.tag =self.tagnumber;
NSLog(#"Tag number for Image2:%d", self.tagnumber);
imageView2.image = [UIImage imageWithData:object.artimageData];
[imageView2 setContentMode:UIViewContentModeScaleAspectFill];
[imageView2 setClipsToBounds:YES];
[cell.contentView addSubview:imageView2];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(165, 100, 145, 30)];
label2.tag = self.tagnumber;
[label2 setFont:[UIFont fontWithName:#"AppleGothic" size:14.0]];
[label2 setTextColor:[UIColor blackColor]];
[label1 setContentMode:UIViewContentModeScaleToFill];
[label2 setTextAlignment:NSTextAlignmentCenter];
label2.text = #"Added 3days ago";;
[label2 setNumberOfLines:1];
[cell.contentView addSubview:label2];
self.tagnumber++;
[self dismissMegaAnnoyingPopup];
return cell;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Hide Category view if not
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//[self performSegueWithIdentifier:#"ShowGalleryDetail" sender:cell];
int rownumber = cell.tag;
//Here i get 0 for rownnumber and even if i use indexpath.row**
NSLog (#"Current Page for article is:%i", rownumber);
}
In your cellForRowAtIndexPath method, you set the tag of a UIImageView or the tag of a UILabel.
In your didSelectRowAtIndexPath method, you get the tag of the cell.
Either get the tag of the image view or label that is in the cell, or set the cell`s tag.