UITableView setSeparatorInset not working as expected

I am trying to create a custom insert for a divider in a UITableView. Here is my code for viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[ChatTableCell class] forCellReuseIdentifier:@"ChatCell"];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 65, 0, 0)];
}

      

Inserting a cell separator does not work correctly as shown. It works for some cells and not others. What am I doing wrong here?

This is how my viewcontroller.m file looks like

#import "ChatListViewController.h"

@interface ChatListViewController ()

@end

@implementation ChatListViewController

- (id)initWithStyle:(UITableViewStyle)style {
    //self = [super initWithStyle:style];
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.parseClassName = kChatRoomClassKey;
        self.paginationEnabled = YES;
        self.pullToRefreshEnabled = YES;
        self.objectsPerPage = 25;
        //[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 65, 0, 0)];
    }
    return self;
}

- (PFQuery *)queryForTable {

    if (![PFUser currentUser]) {
        PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
        [query setLimit:0];
        return query;
    }

    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:kChatRoomUsersKey equalTo:[PFUser currentUser]];
    [query includeKey:kChatRoomUsersKey];
    [query orderByDescending:@"updatedAt"];

    [query setCachePolicy:kPFCachePolicyCacheThenNetwork];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    //
    // If there is no network connection, we will hit the cache first.
    if (self.objects.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
        [query setCachePolicy:kPFCachePolicyCacheThenNetwork];
    }

    return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"ChatCell";

    ChatTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ChatTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSArray *users = [object objectForKey:kChatRoomUsersKey];
    PFUser *cellUser = (PFUser *)[users objectAtIndex:0];
    if ([cellUser.objectId isEqualToString:[PFUser currentUser].objectId]) {
        cellUser = [users objectAtIndex:1];
        NSLog(@"%@", cellUser);
    }
    cell.textLabel.font = [UIFont fontWithName:kDefaultFontKey size:17.0];
    cell.detailTextLabel.font = [UIFont fontWithName:kDefaultFontLightKey size:14];
    if ([cellUser isEqual:[NSNull null]]) {
        cell.username.text = @"Selfie User";
        cell.textLabel.font = [UIFont fontWithName:kDefaultFontKey size:17.0];
        cell.latestText.text = [object objectForKey:kChatRoomLatestTextKey];
        cell.detailTextLabel.font = [UIFont fontWithName:kDefaultFontKey size:10.0];
        [cell.avatarImageView.profileImageView setImage:[UIImage imageNamed:@"AvatarPlaceholder.png"]];
    }
    else {
        cell.username.text = cellUser.username;
        cell.textLabel.font = [UIFont fontWithName:kDefaultFontKey size:17.0];
        cell.latestText.text = [object objectForKey:kChatRoomLatestTextKey];
        cell.detailTextLabel.font = [UIFont fontWithName:kDefaultFontKey size:10.0];
        if ([[cellUser fetchIfNeeded] objectForKey:kPAPUserProfilePicSmallKey] == nil) {
            [cell.avatarImageView.profileImageView setImage:[UIImage imageNamed:@"AvatarPlaceholder.png"]];
        }
        else
            [cell.avatarImageView setFile:[cellUser objectForKey:kPAPUserProfilePicSmallKey]];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PFObject *chatRoomObject = [self.objects objectAtIndex:indexPath.row];
    if ([[chatRoomObject objectForKey:kChatRoomReadKey] isEqual:@NO]) {
        [chatRoomObject setObject:@YES forKey:kChatRoomReadKey];
        [chatRoomObject saveEventually];

        UITabBarItem *tabBarItem = [[self.tabBarController.viewControllers objectAtIndex:PAPFriendsTabBarItemIndex] tabBarItem];

        NSString *currentBadgeValue = tabBarItem.badgeValue;

        if (currentBadgeValue && currentBadgeValue.length > 1) {
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            NSNumber *badgeValue = [numberFormatter numberFromString:currentBadgeValue];
            NSNumber *newBadgeValue = [NSNumber numberWithInt:[badgeValue intValue] - 1];
            tabBarItem.badgeValue = [numberFormatter stringFromNumber:newBadgeValue];
        } else {
            tabBarItem.badgeValue = @"";
        }

    }
    ChatViewController *convoVC = [[ChatViewController alloc] init];
    NSArray *users = [[self.objects objectAtIndex:indexPath.row] objectForKey:kChatRoomUsersKey];
    PFUser *cellUser = (PFUser *)[users objectAtIndex:0];
    if ([cellUser.objectId isEqualToString:[PFUser currentUser].objectId]) {
        cellUser = [users objectAtIndex:1];
    }
    [convoVC setUser:cellUser];
    [convoVC setChatRoom:[self.objects objectAtIndex:indexPath.row]];
    [self.navigationController pushViewController:convoVC animated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[ChatTableCell class] forCellReuseIdentifier:@"ChatCell"];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 65, 0, 0)];
        [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
        [self.navigationItem setTitle:@"Chat"];

        //Change Back button text
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                       initWithTitle: @""
                                       style: UIBarButtonItemStyleBordered
                                       target: nil action: nil];

        [self.navigationItem setBackBarButtonItem: backButton];
        [self.navigationController setDelegate:self];

}


-(void)viewDidAppear:(BOOL)animated {

    [self loadObjects];
    //Google Analytics screen tracking
    id tracker = [[GAI sharedInstance] defaultTracker];
    [tracker set:kGAIScreenName
           value:@"Chat"];
    [tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}

-(void)viewWillAppear:(BOOL)animated {
    [Flurry logEvent:@"Viewed Chat" timed:YES];
}

-(void)viewWillDisappear:(BOOL)animated {
    [Flurry endTimedEvent:@"Viewed Chat" withParameters:nil];
}

@end

      

enter image description here

+3


source to share


1 answer


After all my workarounds now fixed in both iOS 7 and 8.



-(void)viewDidLayoutSubviews
{
  [customTableview setSeparatorInset:UIEdgeInsetsZero];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(customCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]){
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero]; // ios 8 newly added
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
}

      

+7


source







All Articles