Skip to content

Instantly share code, notes, and snippets.

@lint
Last active June 20, 2020 19:45
Show Gist options
  • Save lint/1e301263ca25b422e1ee6a76d899b1f9 to your computer and use it in GitHub Desktop.
Save lint/1e301263ca25b422e1ee6a76d899b1f9 to your computer and use it in GitHub Desktop.
Add custom sections and pages in the official Reddit application's settings page.
@interface BaseView : UIView
@end
@interface BaseLabel : UILabel
+ (id)labelWithSubheaderFont;
@end
@interface AttributedLabelRegular : BaseLabel
@end
@interface RoundedImageView
@property(strong, nonatomic) UIImage *image;
@end
@interface ImageLabelContentView
@property(readonly, nonatomic) AttributedLabelRegular *mainLabel;
@property(readonly, nonatomic) RoundedImageView *imageView;
@end
@interface BaseTableReusableView : BaseView
@property(strong, nonatomic) UIView *contentView;
@end
@interface BaseTableViewCell : UITableViewCell
@property(strong, nonatomic) ImageLabelContentView *imageLabelView;
@end
@interface ImageLabelTableViewCell : BaseTableViewCell
@end
@interface ToggleImageTableViewCell : ImageLabelTableViewCell
@property(strong, nonatomic) UISwitch *accessorySwitch;
@end
%hook AppSettingsViewController
NSInteger customSectionIndex = 2; //where the section will be placed (2 to 7)
NSInteger numCustomSections = 1; //the amount of custom sections added
//there appears to be an empty section at index 1 for some reason. Best option is to have custom section at index 2 or greater
- (NSInteger)numberOfSectionsInTableView:(UITableView *)arg1 {
return %orig + numCustomSections;
}
- (NSInteger)tableView:(UITableView *)arg1 numberOfRowsInSection:(NSInteger)arg2 {
if (arg2 < customSectionIndex) {
return %orig;
} else if (arg2 > customSectionIndex) {
return %orig(arg1, arg2 - numCustomSections); //offsetting original section values
} else {
return 1; //the number of cells in the custom section
}
}
- (void)tableView:(UITableView *)arg1 didSelectRowAtIndexPath:(NSIndexPath *)arg2 {
if (arg2.section < customSectionIndex) {
%orig;
} else if (arg2.section > customSectionIndex) {
NSIndexPath *offsetPath = [NSIndexPath indexPathForRow:arg2.row inSection:arg2.section - numCustomSections]; //offsetting original section values
%orig(arg1, offsetPath);
} else {
//tapped a custom cell
}
}
- (UITableViewCell *)tableView:(UITableView *)arg1 cellForRowAtIndexPath:(NSIndexPath *)arg2 {
if (arg2.section < customSectionIndex) {
return %orig;
} else if (arg2.section > customSectionIndex) {
NSIndexPath *offsetPath = [NSIndexPath indexPathForRow:arg2.row inSection:arg2.section - numCustomSections]; //offsetting original section values
return %orig(arg1, offsetPath);
} else { //custom section cells
//make the cells
ToggleImageTableViewCell *cell = [arg1 dequeueReusableCellWithIdentifier:@"kToggleCellID" forIndexPath:arg2];
cell.imageLabelView.mainLabel.text = @"Section Cell";
UIImage *cellImage = [UIImage imageNamed:@"icon_redditor_20"];
cellImage = [cellImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
cell.imageLabelView.imageView.image = cellImage;
return cell;
}
}
- (UIView *)tableView:(UITableView *)arg1 viewForHeaderInSection:(NSInteger)arg2 {
if (arg2 < customSectionIndex) {
return %orig;
} else if (arg2 > customSectionIndex) {
return %orig(arg1, arg2 - numCustomSections); //offsetting original section values
} else { //custom section header view
BaseTableReusableView *orig = (BaseTableReusableView *)%orig;
BaseLabel *label = [[orig contentView] subviews][0];
label.text = @"SECTION TITLE"; //custom section's title text
return orig;
}
}
%end
@interface BaseView : UIView
@end
@interface BaseLabel : UILabel
+ (id)labelWithSubheaderFont;
@end
@interface AttributedLabelRegular : BaseLabel
@end
@interface RoundedImageView
@property(strong, nonatomic) UIImage *image;
@end
@interface ImageLabelContentView
@property(readonly, nonatomic) AttributedLabelRegular *mainLabel;
@property(readonly, nonatomic) RoundedImageView *imageView;
@end
@interface BaseTableReusableView : BaseView
@property(strong, nonatomic) UIView *contentView;
@end
@interface BaseTableViewCell : UITableViewCell
@property(strong, nonatomic) ImageLabelContentView *imageLabelView;
@end
@interface ImageLabelTableViewCell : BaseTableViewCell
@end
@interface ToggleImageTableViewCell : ImageLabelTableViewCell
@property(strong, nonatomic) UISwitch *accessorySwitch;
@end
%hook AppSettingsViewController
NSInteger customSectionIndex = 2; //where the section will be placed (2 to 7)
NSInteger numCustomSections = 1; //the amount of custom sections added
//there appears to be an empty section at index 1 for some reason. Best option is to have custom section at index 2 or greater
- (NSInteger)numberOfSectionsInTableView:(UITableView *)arg1 {
return %orig + numCustomSections;
}
- (NSInteger)tableView:(UITableView *)arg1 numberOfRowsInSection:(NSInteger)arg2 {
if (arg2 < customSectionIndex) {
return %orig;
} else if (arg2 > customSectionIndex) {
return %orig(arg1, arg2 - numCustomSections); //offsetting original section values
} else {
return 2; //the number of cells in the custom section
}
}
- (void)tableView:(UITableView *)arg1 didSelectRowAtIndexPath:(NSIndexPath *)arg2 {
if (arg2.section < customSectionIndex) {
%orig;
} else if (arg2.section > customSectionIndex) {
NSIndexPath *offsetPath = [NSIndexPath indexPathForRow:arg2.row inSection:arg2.section - numCustomSections]; //offsetting original section values
%orig(arg1, offsetPath);
} else {
//tapped a custom cell
}
}
- (UITableViewCell *)tableView:(UITableView *)arg1 cellForRowAtIndexPath:(NSIndexPath *)arg2 {
if (arg2.section < customSectionIndex) {
return %orig;
} else if (arg2.section > customSectionIndex) {
NSIndexPath *offsetPath = [NSIndexPath indexPathForRow:arg2.row inSection:arg2.section - numCustomSections]; //offsetting original section values
return %orig(arg1, offsetPath);
} else { //custom section cells
//make the cells
ToggleImageTableViewCell *cell = [arg1 dequeueReusableCellWithIdentifier:@"kToggleCellID"];
UIImage *cellImage = [UIImage imageNamed:@"icon_redditor_20"];
cellImage = [cellImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
cell.imageLabelView.imageView.image = cellImage;
if (arg2.row == 0){
cell.imageLabelView.mainLabel.text = @"Section Cell Test 1";
} else if (arg2.row == 1){
cell.imageLabelView.mainLabel.text = @"Section Cell Test 2";
}
return cell;
}
}
- (UIView *)tableView:(UITableView *)arg1 viewForHeaderInSection:(NSInteger)arg2 {
if (arg2 < customSectionIndex) {
return %orig;
} else if (arg2 > customSectionIndex) {
return %orig(arg1, arg2 - numCustomSections); //offsetting original section values
} else { //custom section header view
BaseTableReusableView *orig = (BaseTableReusableView *)%orig;
BaseLabel *label = [[orig contentView] subviews][0];
label.text = @"SECTION TITLE"; //custom section's title text
return orig;
}
}
%end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment