UITableViewCellのアクセサリがUITableViewCellAccessoryCheckmarkに設定されている場合に表示されるデフォルトのチェックマーク画像を置き換えたいのですが。
だから私はまだ書きたいです:
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
ただし、デフォルトではなく、自分の画像を表示したいのですが。
どんな助けでも大歓迎です。
UITableViewCellのアクセサリがUITableViewCellAccessoryCheckmarkに設定されている場合に表示されるデフォルトのチェックマーク画像を置き換えたいのですが。
だから私はまだ書きたいです:
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
ただし、デフォルトではなく、自分の画像を表示したいのですが。
どんな助けでも大歓迎です。
myUITableViewCell.accessoryView = myImageView;
次に、画像をUIImageViewに配置します。
見る
方法1:
使えると思います
チェックマーク画像付きのUIButtonがあるとします。
の上cellForRowAtIndexPath:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
checkButtonTapped:event:メソッド:
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
AccessoriesButtonTappedForRowWithIndexPath:メソッド
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
私はこのリンクから上記のコードを取得しました:
iPhoneでUITableViewのカスタムアクセサリビューを実装する
方法2:
また、カスタムtableViewセルを使用して作成する方法もあります。
カスタムセルを作成し、セルの右側にimageViewを追加する必要があると思います。
このimageViewは、必要な画像を保持します。
didSelectRowAtRowAtIndexPathの場合:タップ数に基づいて画像を追加および削除できます。
さらにサポートが必要な場合はお知らせください。
お役に立てれば。
このコードを見てください:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSMutableDictionary *item = [dataArray bjectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"text"];
[item setObject:cell forKey:@"cell"];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
...
}
- (void)checkButtonTapped:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil) {
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}