Skip to content

Commit c269536

Browse files
authored
Update RNTableView.m
1 parent 92c6fc7 commit c269536

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

RNTableView/RNTableView.m

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "RNTableFooterView.h"
1717
#import "RNTableHeaderView.h"
1818
#import "RNReactModuleCell.h"
19+
#import <AVFoundation/AVFoundation.h>
1920

2021
@interface RNTableView()<UITableViewDataSource, UITableViewDelegate> {
2122
id<RNTableViewDatasource> datasource;
@@ -107,7 +108,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge {
107108
_autoFocus = YES;
108109
_autoFocusAnimate = YES;
109110
_allowsToggle = NO;
110-
_allowsMultipleSelection = NO;
111+
_allowsMultipleSelection = YES;
111112
}
112113
return self;
113114
}
@@ -209,7 +210,7 @@ - (void)createTableView {
209210
_tableView.dataSource = self;
210211
_tableView.delegate = self;
211212
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
212-
_tableView.allowsMultipleSelectionDuringEditing = NO;
213+
_tableView.allowsMultipleSelectionDuringEditing = YES;
213214
_tableView.contentInset = self.contentInset;
214215
_tableView.contentOffset = self.contentOffset;
215216
_tableView.scrollIndicatorInsets = self.scrollIndicatorInsets;
@@ -347,6 +348,9 @@ -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel
347348
}
348349

349350
}
351+
352+
// Every cell can have it's own text color.
353+
cell.textLabel.textColor = [RCTConvert UIColor:item[@"textColor"]];
350354

351355
if (self.selectedBackgroundColor && [item[@"selected"] intValue])
352356
{
@@ -377,6 +381,38 @@ -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel
377381
UIGraphicsEndImageContext();
378382
} else {
379383
cell.imageView.image = image;
384+
385+
// Load artwork.
386+
NSURL *fileURL = [NSURL fileURLWithPath:item[@"filePath"]];
387+
AVAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
388+
for (NSString *format in [asset availableMetadataFormats]) {
389+
for (AVMetadataItem *item in [asset metadataForFormat:format]) {
390+
if ([[item commonKey] isEqualToString:@"artwork"]) {
391+
UIImage *artwork = nil;
392+
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
393+
artwork = [UIImage imageWithData:[item.value copyWithZone:nil]];
394+
}
395+
else { // if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
396+
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
397+
artwork = [UIImage imageWithData:item.dataValue];
398+
} else {
399+
NSDictionary *dict;
400+
[item.value copyWithZone:nil];
401+
artwork = [UIImage imageWithData:[dict objectForKey:@"data"]];
402+
}
403+
}
404+
cell.imageView.image = artwork;
405+
}
406+
}
407+
}
408+
409+
// Custom size.
410+
CGSize itemSize = CGSizeMake(48, 48);
411+
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
412+
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
413+
[cell.imageView.image drawInRect:imageRect];
414+
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
415+
UIGraphicsEndImageContext();
380416
}
381417
}
382418

@@ -496,9 +532,29 @@ -(UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS
496532
if (item[@"selectionStyle"] != nil) {
497533
cell.selectionStyle = [RCTConvert int:item[@"selectionStyle"]];
498534
}
535+
536+
// Add a custom accessory button.
537+
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
538+
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
539+
[button setTitle:@"\uF1D8" forState:UIControlStateNormal];
540+
[button setTitleColor:[RCTConvert UIColor:item[@"accessoryColor"]] forState:UIControlStateNormal];
541+
[button.titleLabel setFont:[UIFont fontWithName: @"Material Design Icons" size: 22.0f]];
542+
button.frame = CGRectMake(0, 0, 48, 64);
543+
cell.accessoryView = button;
544+
499545
return cell;
500546
}
501547

548+
- (void)checkButtonTapped:(id)sender event:(id)event{
549+
NSSet *touches = [event allTouches];
550+
UITouch *touch = [touches anyObject];
551+
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
552+
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
553+
if (indexPath != nil){
554+
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
555+
}
556+
}
557+
502558
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
503559
return _sections[section][@"label"];
504560
}
@@ -524,7 +580,9 @@ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPat
524580
}
525581

526582
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
527-
[tableView deselectRowAtIndexPath:indexPath animated:NO];
583+
if (!_editing) {
584+
[tableView deselectRowAtIndexPath:indexPath animated:NO];
585+
}
528586

529587
NSMutableDictionary *newValue = [self dataForRow:indexPath.item section:indexPath.section];
530588
newValue[@"target"] = self.reactTag;

0 commit comments

Comments
 (0)