如何使用/导入SWTableViewCell库

时间:2014-05-26 14:53:33

标签: objective-c xcode

我想使用第三方图书馆" Here"。

我的代码没有产生任何错误,它可以显示单元格,但滑动不起作用。我怀疑是,我在导入图书馆时遗漏了一些东西。

我已经阅读了如何在" Here"中实现此库。和" Here"。但他们都没有告诉我如何导入图书馆。

" Here"是我的项目结构,显示我如何导入库:

viewController.h

#import <UIKit/UIKit.h>
#import "SWTableViewCell.h"

@interface ViewController : UITableViewController <UITableViewDelegate,         UITableViewDataSource, SWTableViewCellDelegate>

@end

viewController.m

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.letterData count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath {
 NSLog(@"cell selected at index path %ld:%ld", (long)indexPath.section,   (long)indexPath.row);
NSLog(@"selected cell index path is %@", [self.tableView indexPathForSelectedRow]);

if (!tableView.isEditing) {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *cellIdentifier = @"Cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

static NSString *cellIdentifier = @"Cell";

SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {

    cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:cellIdentifier];
    cell.leftUtilityButtons = [self leftButtons];
    cell.rightUtilityButtons = [self rightButtons];
    cell.delegate = self;
}

cell.textLabel.text = self.letterData[indexPath.row];

return cell;

return cell;
}


- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
                                            title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                            title:@"Delete"];

return rightUtilityButtons;
}


- (NSArray *)leftButtons
{
NSMutableArray *leftUtilityButtons = [NSMutableArray new];

[leftUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
                                            icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
                                            icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
                                            icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
 [UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
                                            icon:[UIImage imageNamed:@"list.png"]];

return leftUtilityButtons;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell     forRowAtIndexPath:(NSIndexPath *)indexPath {
// Set background color of cell here if you don't want default white
}

#pragma mark - SWTableViewDelegate

- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
switch (state) {
    case 0:
        NSLog(@"utility buttons closed");
        break;
    case 1:
        NSLog(@"left utility buttons open");
        break;
    case 2:
        NSLog(@"right utility buttons open");
        break;
    default:
        break;
}
}

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index
{
switch (index) {
    case 0:
        NSLog(@"left button 0 was pressed");
        break;
    case 1:
        NSLog(@"left button 1 was pressed");
        break;
    case 2:
        NSLog(@"left button 2 was pressed");
        break;
    case 3:
        NSLog(@"left btton 3 was pressed");
    default:
        break;
}
}

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
switch (index) {
    case 0:
    {
        NSLog(@"More button was pressed");
        UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"More more more" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];
        [alertTest show];

        [cell hideUtilityButtonsAnimated:YES];
        break;
    }
    case 1:
    {
        // Delete button was pressed
        NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];

//            [_testArray[cellIndexPath.section] removeObjectAtIndex:cellIndexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath]     withRowAnimation:UITableViewRowAnimationLeft];
        break;
    }
    default:
        break;
}
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
// allow just one cell's utility button to be open at once
return YES;
}

- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
switch (state) {
    case 1:
        // set to NO to disable all left utility buttons appearing
        return YES;
        break;
    case 2:
        // set to NO to disable all right utility buttons appearing
        return YES;
        break;
    default:
        break;
}

return YES;
}

0 个答案:

没有答案