当前位置 : 主页 > 手机开发 > ios >

ios – UITableView setSeparatorInset无法正常工作

来源:互联网 收集:自由互联 发布时间:2021-06-11
我试图在UITableView中为分隔符设置自定义插件.这是我的viewDidLoad代码 - (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[ChatTableCell class] forCellReuseIdentifier:@"ChatCell"]; [self.tableView se
我试图在UITableView中为分隔符设置自定义插件.这是我的viewDidLoad代码

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

单元格分隔符插图无法正常工作,如图所示.它适用于某些细胞而不适用于其他细胞.我在这做错了什么?

这是我的viewcontroller.m文件的样子

#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
在我的所有解决方法之后,现在已经在ios 7和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];
}
}
网友评论