Reputation: 143
My project is based on parsing XML data and displaying screen, In my project I need to set UIButton programaitcally inside a table view cell. I done it but when I run it the button repeats twice. Like this
I dont know to solve this Here is the code i tried
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return [eventarray count];
if (section == 1)
return 1;
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"eventCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(indexPath.section == 0)
{
Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
cell.textLabel.text = msglist.invitationdet;
NSLog(@"Array %@",[SingleTonClass sinlgeTon].colorArray);
NSInteger stat=msglist.readflag;
if([[SingleTonClass sinlgeTon].colorArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat == 1 ) {
NSInteger stat1 = msglist.responseflag;
if(stat1 == 1){
cell.textLabel.textColor = [UIColor yellowColor];
}
else {
cell.textLabel.textColor = [UIColor redColor];
}
}
else{
cell.textLabel.textColor = [UIColor greenColor];
}
cell.backgroundColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.section == 1)
{
UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
[viewmoreButton setTitle:@"View More" forState:UIControlStateNormal];
[cell addSubview:viewmoreButton];
[viewmoreButton addTarget:self
action:@selector(viewMore:)
forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [ UIColor blackColor];
}
return cell;
}
The button has to show only on second cell, Please help me how to solve this Thanks in Advance
Upvotes: 1
Views: 919
Reputation: 1097
Replace [cell addSubview:viewmoreButton]; with [cell.contentView addSubview:viewmoreButton];
In Detail
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
if (indexPath.section==0) {
Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
cell.textLabel.text = msglist.invitationdet;
NSLog(@"Array %@",[SingleTonClass sinlgeTon].colorArray);
NSInteger stat=msglist.readflag;
if([[SingleTonClass sinlgeTon].colorArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat == 1 ) {
NSInteger stat1 = msglist.responseflag;
if(stat1 == 1){
cell.textLabel.textColor = [UIColor yellowColor];
}
else {
cell.textLabel.textColor = [UIColor redColor];
}
}
else{
cell.textLabel.textColor = [UIColor greenColor];
}
cell.backgroundColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section==1) {
UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
[viewmoreButton setTitle:@"View More" forState:UIControlStateNormal];
[cell.contentView addSubview:viewmoreButton];
cell.textLabel.text =alphaArray[indexPath.row];
}
return cell;
}
Upvotes: 0
Reputation: 3117
This is what expected to happen because the table view is reusing the cell with the identifier you mentioned. There are two ways to avoid this :
1) User two different prototype cells (if you are using Interface Builder which I think you are) with different identifiers and getting a reference for the cell according to the required one.
2) If you don't want to change this approach of yours, it can also make the same thing happen but with slight modification. First of all, you are adding a button to the cell on every call of tableView:cellForRowAtIndexPath:
method which is simply wrong. What you can do is check if the button already exist and if its not, only create it then.
UIButton *btn = (UIButton*)[cell.contentView viewWithTag:2000];
if(btn == nil){
// Your button code here
[btn setTag:2000];
[cell.contentView addSubview:btn];
}
Now simply check if its the 0th section, get the button's reference and hide it, and if its 1st section, unhide it.
Upvotes: 1
Reputation: 15335
Might causing due to the Reusability of the Cell, So initialise the UITableViewCell within the sections block
Try this :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"eventCell";
UITableViewCell *cell ;
if(indexPath.section == 0)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
cell.textLabel.text = msglist.invitationdet;
// .... . ...
}
if(indexPath.section == 1)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// ........
}
return cell;
}
Upvotes: 1
Reputation: 2369
I think the issue is that you're adding the button to the UITableViewCell
, and then when reloadData
is called on the table, the cell is queued for reuse with that button still attached.
Create your own UITableViewCell
subclass and use that for the section of your table that will require a custom button.
Upvotes: 2