cell with imageview and button getting reused - ios

I have a table view with many cells. In the first cell I placed a UIImageView to hold a pic and next to it a button that will open up a popOver.
My question is: Why is this cell is getting reused in spite of checking (cell==nil) while others are working fine.
The code is as below.
P.S. This is without using storyboard and xib only programatically.
- (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];
}
if (indexPath.section == 0)
{
if(indexPath.row==0)
{
if(self.imageView==nil)
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 2, 50, 40)];
self.imageView.image = [UIImage imageNamed:#"user.png"];
self.imageView.backgroundColor=[UIColor blackColor];
}
[cell.contentView addSubview:self.imageView];
if(self.chooseImage==nil)
{
self.chooseImage = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 80, 30)];
[self.chooseImage addTarget:self action:#selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
self.chooseImage.backgroundColor = [UIColor blackColor];
[self.chooseImage setTitle:#"CHOOSE" forState:UIControlStateNormal];
}
[cell.contentView addSubview:self.chooseImage];
}

First in code add at end of the method return cell; and use following 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];
if(self.imageView==nil)
{
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 2, 50, 40)];
self.imageView.image = [UIImage imageNamed:#"user.png"];
self.imageView.backgroundColor=[UIColor blackColor];
}
[cell.contentView addSubview:self.imageView];
if(self.chooseImage==nil)
{
self.chooseImage = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 80, 30)];
[self.chooseImage addTarget:self action:#selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
self.chooseImage.backgroundColor = [UIColor blackColor];
[self.chooseImage setTitle:#"CHOOSE" forState:UIControlStateNormal];
}
[cell.contentView addSubview:self.chooseImage];
}
return cell; // this line is not in your code so add this line.
}

Related

Radio Button issue in Objective c

I have a table view in which I'm loading some data i'm trying to make radio button on that data, but when i select any cell it does not deselect other it is working as a check box . I'm confused where i'm doing mistake, My code is this,
-(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];
radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 60, 30, 30);
[radiobtn setImage:[UIImage imageNamed:#"circle.png"] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:#"rights.png"] forState:UIControlStateSelected];
[radiobtn addTarget:self action:#selector(radiobtn:)
forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:1];
cell.selectedBackgroundView = selectionColor;
cell.selectionStyle = UITableViewCellDragStateLifting;
return cell;
}
-(void)radiobtn:(UIButton *)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
}
else
{
[sender setSelected:YES];
}
}
It is looking like this,
Try this.
-(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];
UIButton *radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 60, 30, 30);
radiobtn.userInteractionEnabled = NO;
[radiobtn setImage:[UIImage imageNamed:#"circle.png"] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:#"rights.png"] forState:UIControlStateSelected];
// [radiobtn addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:1];
cell.selectedBackgroundView = selectionColor;
cell.selectionStyle = UITableViewCellDragStateLifting;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
button.selected = YES;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
button.selected = NO;
}

UITableViewCells with UIButton overlaps while scrolling

I need to create a table view with some buttons as its elements, view gets loaded fine, but while scrolling down buttons gets overlapped. I think old buttons are not cleared and new buttons are placing over it. Please help me to fix this problem. Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
return cell;
}
just replace the line
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
is now
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
(or) //use this in cell for rowatindexpath
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if(cell == nil)
//{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
//}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
}
return cell;
}
If you use Xib you can use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
I'd suggest using tags for this. See the code below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSInteger buttonTag = 100500;
UIButton *button = [cell.contentView viewWithTag: buttonTag]
if(button == nil)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
}
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
return cell;
}
Please insert below code just before you add button as subview, it will remove all the buttons in your content view of cell before you add a new one.
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}

UITableView can not show correct color of button in iOS

I used BVReorderTableView https://github.com/bvogelzang/BVReorderTableView to reorder row in tableview. But i have a problem: When I add UIButton to each row, this button always show clear color.Once selected row, this button just shown.
I tried this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[prioritybtn setFrame:CGRectMake(220, 0, 70, 32)];
[prioritybtn setTag:4000];
[cell.contentView addSubview:prioritybtn];
}
UIButton *priorityButton = (UIButton*)[cell.contentView viewWithTag:4000];
if ([[items objectAtIndex:indexPath.row] isKindOfClass:[NSString class]] &&
[[items objectAtIndex:indexPath.row] isEqualToString:#"DUMMY"]) {
cell.textLabel.text = #"";
cell.contentView.backgroundColor = [UIColor clearColor];
prioritybtn.hidden= YES;
}
else {
cell.textLabel.text = [items objectAtIndex:indexPath.row];
[priorityButton setSelected:NO];
[priorityButton setTitle:#"Priority" forState:UIControlStateNormal];
priorityButton.enabled = YES;
}
return cell;
}
Also, I want to hide button only at first row, other row is still shown. How can i do that? Thanks
To hide the button at first row:
if (indexPath.row == 1){
prioritybtn.hidden = YES;
prioritybtn.userInteractionEnabled = NO;
} else {
prioritybtn.hidden = NO;
prioritybtn.userInteractionEnabled = YES;
}
Add this before return cell;
Regarding the color, add this where you initialize the button:
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
prioritybtn.backgroundColor = [UIColor greenColor]; //put the color you want
Try like this:-
cell.selectionStyle =
UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
UIButton *reletedbtn=nil;
UILabel *Nameoflb=nil;
cell = [tableeample dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Nameoflb = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,40)];
Nameoflb.backgroundColor=[UIColor clearColor];
Nameoflb.textAlignment=NSTextAlignmentCenter;
Nameoflb.textColor=[UIColor blackColor];
[Nameoflb setFont:[UIFont fontWithName:#"Helvetica-Bold" size :14]];
Nameoflb.text=[dataarray objectAtIndex:indexPath.row];
[[cell contentView] addSubview:Nameoflb];
if (indexPath.row!=0)
{
reletedbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
reletedbtn.frame=CGRectMake(Nameoflb.frame.origin.x+Nameoflb.frame.size.width, Nameoflb.frame.origin.y+10,40, 25);
reletedbtn.tintColor=[UIColor redColor];
[reletedbtn setTitle:#"butoon" forState:UIControlStateNormal];
//[reletedbtn addTarget:self action:#selector(statusMasseage:) forControlEvents:UIControlEventTouchUpInside];
//reletedbtn.tag=1;
[[cell contentView] addSubview:reletedbtn];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

How to change tableview cell image?

I have table view in side bar which is similar to facebook app. In my sidebar tableview i have imageview in each row. I want the imageview will be changed when i clicked the cell and it will be retain while for selecting another cell. Here is my 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.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}
Try this in your didSelectRowAtIndexPath delegate method :-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"yourImage"];
Try this in cellForRowAtIndexPath.
UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image"]];
cell.selectedBackgroundView = selectedImageView;
Need to capture the selected indexpath in didSelectRowAtIndexPath: method and need to reload the tableview in that method. In cellForRowAtIndexPath need to check the selected index with current index.
- (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.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
[cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}

UITableView dequeue not working as expected

As I scroll down the list, all the rows are there, but they keep adding more subviews the more they appear on the visible frame
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
}
NSUInteger row = indexPath.row;
[self createCell: cell onRow: row];
return cell;
}
- (void) createCell: (UITableViewCell*)cell onRow: (NSUInteger)row
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I think you need to execute almost all of the logic that's in createCell: only within the if (cell == nil){ segment of your code. The part that should execute where you're currently calling createCell: is just getting a reference to the titleLabel and setting its text value.
To clarify, here's the kind of modification I'm suggesting (not tested, but should give the right idea):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
[self setUpCell: cell];
}
NSUInteger row = indexPath.row;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:42];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
return cell;
}
- (void) setUpCell: (UITableViewCell*)cell
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.tag = 42;
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}

Resources