谁能告诉我如何使用具有不同图像和动作的自定义单元格在 UITableView 中制作按钮。我试过这样。我根据字典中的值添加按钮图像和操作,它第一次正常工作,但滚动后,按钮图像发生变化。
- (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;
}
}