Reputation: 349
Can anyone please tell me how can we make the buttons in UITableView with custom cell with different images and action.I have tried like this. I am adding button image and action according to the values in my dictionary,it works properly for first time but after scrolling ,the button images changes.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
{
static NSString *CellIdentifier = @"MediaPickerCustomCell";
MediaPickerCustomCell *customCell = (MediaPickerCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(customCell == nil){
}
[customCell._titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[customCell._titleLabel setMinimumFontSize:FONT_SIZE];
[customCell._titleLabel setNumberOfLines:0];
[customCell._titleLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[customCell._titleLabel setTag:1];
NSString *text = [eventsarray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[customCell._titleLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
customCell._titleLabel.attributedText=[attributedarrayevents objectAtIndex:indexPath.row];
CGFloat aHeight = [tableView rectForRowAtIndexPath:indexPath].size.height;
CGRect frame= customCell.priorview.frame;
frame.size.height=aHeight;
[customCell.priorview setFrame:frame];
//code to setframe of btn register
CGFloat aHeight1;
CGRect frame1= customCell._titleLabel.frame;
aHeight1=frame1.size.height+frame1.origin.y-40;
CGRect frame2= customCell.addEventBtn.frame;
frame2.origin.y=aHeight1;
[customCell.addEventBtn setFrame:frame2];
[customCell.addEventBtn setHidden:NO];
if([[[dataEventArray objectAtIndex:indexPath.row]objectForKey:@"mandatory"]isEqualToString:@"1"]){
[customCell.addEventBtn setImage:[UIImage imageNamed:@"registered.png"] forState:UIControlStateNormal];
[customCell.addEventBtn setUserInteractionEnabled:NO];
}
else if ([[[dataEventArray objectAtIndex:indexPath.row]objectForKey:@"mandatory"]isEqualToString:@"0"]){
[customCell.addEventBtn addTarget:self action:@selector(addeventToCaendar) forControlEvents:UIControlEventTouchUpInside];
[customCell.addEventBtn setBackgroundImage:[UIImage imageNamed:@"register_btn.png"] forState:UIControlStateNormal];
[customCell.addEventBtn setTitle:@"Register" forState:UIControlStateNormal];
[customCell.addEventBtn setUserInteractionEnabled:YES];
}
if([[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"priority"] isEqualToString:@"1"]){
customCell.priorview.backgroundColor=[UIColor colorWithRed:255/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
}
else if([[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"priority"] isEqualToString:@"2"]){
customCell.priorview.backgroundColor=[UIColor colorWithRed:237/255.0 green:206/255.0 blue:112/255.0 alpha:1.0];
}
else{
customCell.priorview.backgroundColor=[UIColor colorWithRed:124/255.0 green:197/255.0 blue:118/255.0 alpha:1.0];
}
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeCalledEvents:) ];
customCell.tag=[[[dataEventArray objectAtIndex:indexPath.row] objectForKey:@"id"] intValue];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[customCell addGestureRecognizer:gesture];
return customCell;
}
}
Upvotes: 0
Views: 87
Reputation: 3082
This usually happens when it starts repeating the images.
please try this code :-
if (cell == nil) {
cell = [[MediaPickerCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
for (id obj in cell.contentView.subviews){
[obj removeFromSuperview];
}
Hope this will help you out.If not let me know..
Upvotes: 0
Reputation: 11754
Try changing:
if(customCell == nil){
}
To:
if(!customCell)
customCell = [[MediaPickerCustomCell alloc] initWithStyle:nil reuseIdentifier:CellIdentifier];
That'll correctly allocate the cell for reuse, which stops it from dequeuing the cells in the wrong way. For the initWithStyle
you can try the various types, but if it's all custom you might have created your own is why I left it as nil in the example.
Upvotes: 1