テーブル ビューでカスタム クラスを使用してセルを作成する
MyCustomCell.h
#import <UIKit/UIKit.h>
@interface MyCustomCell : UITableViewCell
@end
MyCustomCell.m
#import "MyCustomCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.selectionStyle = UITableViewCellSelectionStyleBlue;
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(5,4,80,80);
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.cornerRadius = 5.0f;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
float imgWidth = self.imageView.image.size.width;
if(imgWidth > 0)
{
self.textLabel.frame = CGRectMake(110,2,170,25);
self.detailTextLabel.frame = CGRectMake(110,18,200,25);
self.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
self.detailTextLabel.alpha = 0.0;
self.textLabel.font = [UIFont boldSystemFontOfSize:12];
self.textLabel.textColor = [UIColor colorWithRed:.2314 green:.3490 blue:.5961 alpha:1.0];
}
}
@end
次のように使用します。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setBackgroundImage:[UIImage imageNamed:@"select2x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(110, 25, 63, 32);
//btn.tag = indexPath.row;
viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewProfile.frame = CGRectMake(190, 25, 93, 32);
//[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
[viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile@2x.png"] forState:UIControlStateNormal];
[viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:viewProfile];
[cell.contentView addSubview:btn];
}
else
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(110, 25, 63, 32);
//btn.tag = indexPath.row;
viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewProfile.frame = CGRectMake(190, 25, 93, 32);
//[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
[viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile.png"] forState:UIControlStateNormal];
[viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:viewProfile];
[cell.contentView addSubview:btn];
}
return cell;
}
上記のように作成されたように、各セルにはアクションを実行する2つのボタンが含まれています(セルタップではなくボタンタップでアクションを実行します)
今私が欲しいのは、ユーザーが特定のセルのボタン(たとえば、選択ボタン)をタップすると、その特定のセルのみが強調表示または陰影または色付けされ、ユーザーが別のセルのボタンをタップすると、そのセルが強調表示され、前のセルが強調表示されていません。前と次のインデックスパスを2つの異なる変数に保存しようとしましたが、それらを操作して目的を達成することはできません。
- (void)SelectButtonTapped:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [homeTable indexPathForCell:cell];
// UITableViewCell *cell1 = [homeTable cellForRowAtIndexPath:indexPath];
//currentSelection = indexPath.row;
//MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
[[homeTable delegate]tableView:homeTable didSelectRowAtIndexPath:indexPath];
//MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
/*if(currentSelection == 0)
{
[cell1 setSelected:YES animated:YES];
currentSelection++;
}
else
{
[cell1 setSelected:YES animated:YES];
}*/
// NSIndexPath *indexPath1 = [homeTable indexPathForCell:cell];
NSLog(@"current===>%d",currentSelection);
//[cell1 setSelected:NO animated:YES];
//if(currentSelection == indexPath)
//UIButton *clickButton = (UIButton *)sender;
// NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:button.tag inSection:0];
//button.tag = indexPathHighlight.row;
//UITableViewCell *newCell = [homeTable cellForRowAtIndexPath:indexPathHighlight];
//if(newCell.selected == NO)
//{
//[newCell setSelected:YES animated:YES];
// [newCell setBackgroundColor:[UIColor lightGrayColor]];
//}
//else
// {
//[newCell setSelected:NO animated:YES];
//[newCell setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
//}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
cell1.selectionStyle= UITableViewCellSelectionStyleBlue;
}
私の乱雑なボタンアクションメソッドを見ることができます。多くのオプションを試しましたが、成功しませんでした。これについて助けてください。