无法在NSObject Xcode中使用UIView

时间:2015-07-27 11:13:34

标签: ios xcode

#import <Foundation/Foundation.h>

typedef enum _NSBubbleType
{
BubbleTypeMine = 0,
BubbleTypeSomeoneElse = 1
} NSBubbleType;

@interface NSBubbleData : NSObject

@property (readonly, nonatomic, strong) NSDate *date;
@property (readonly, nonatomic) NSBubbleType type;
@property (readonly, nonatomic, strong) UIView *view;
@property (readonly, nonatomic) UIEdgeInsets insets;
@property (nonatomic, strong) UIImage *avatar;

- (id)initWithText:(NSString *)text date:(NSDate *)date type:      (NSBubbleType)type;
+ (id)dataWithText:(NSString *)text date:(NSDate *)date type:(NSBubbleType)type;
- (id)initWithImage:(UIImage *)image date:(NSDate *)date type:(NSBubbleType)type;
+ (id)dataWithImage:(UIImage *)image date:(NSDate *)date type: (NSBubbleType)type;
- (id)initWithView:(UIView *)view date:(NSDate *)date type:(NSBubbleType)type insets:(UIEdgeInsets)insets;
+ (id)dataWithView:(UIView *)view date:(NSDate *)date type:(NSBubbleType)type insets:(UIEdgeInsets)insets;

下面是我的对象和.m类     #import“NSBubble.h”     #import

@implementation NSBubble

@synthesize datee,vieww,insetss,typee;

#pragma mark - Text bubble

const UIEdgeInsets textInsetsMine = {5, 10, 11, 17};
const UIEdgeInsets textInsetsSomeone = {5, 15, 11, 10};

+ (id)dataWithText:(NSString *)text date:(NSDate *)date type:(NSBubbleType)type
{
    return [[NSBubble alloc] initWithText:text date:date type:type];
}

- (id)initWithText:(NSString *)text date:(NSDate *)date type:(NSBubbleType)type
{
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = (text ? text : @"");
label.font = font;
label.backgroundColor = [UIColor clearColor];

UIEdgeInsets insets = (type == BubbleTypeMine ? textInsetsMine : textInsetsSomeone);
return [self initWithView:label date:date type:type insets:insets];
}


#pragma mark - Custom view bubble

+ (id)dataWithView:(UIView *)view date:(NSDate *)date type:(NSBubbleType)type insets:(UIEdgeInsets)insets
{
return [[NSBubble alloc] initWithView:view date:date type:type insets:insets];
}

- (id)initWithView:(UIView *)view date:(NSDate *)date type:(NSBubbleType)type insets:(UIEdgeInsets)insets
{
self = [super init];
if (self)
{

    vieww = view;
    datee = date;

    typee = type;
    insetss = insets;
}
return self;
}


@end

这是我的表格视图

#import <UIKit/UIKit.h>

#import "HeaderDataSource.h"
#import "NSBubbleTblCell.h"

@interface NSBubbleTblView : UITableView      <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, assign) IBOutlet  id<UIBubbleTableViewDataSource> bubbleDataSource;
@property (nonatomic) NSTimeInterval snapInterval;

- (void) scrollBubbleViewToBottomAnimated:(BOOL)animated;

这是.m

#import "NSBubbleTblView.h"
#import "NSBubble.h"
#import "NSBubbleTblHeaderCell.h"

@interface NSBubbleTblView ()

@property (nonatomic, retain) NSMutableArray *bubbleSection;

@end

@implementation NSBubbleTblView

@synthesize dataSource,snapInterval,bubbleSection;

#pragma mark - Initializators

- (void)initializator
{
// UITableView properties

self.backgroundColor = [UIColor clearColor];
self.separatorStyle = UITableViewCellSeparatorStyleNone;
assert(self.style == UITableViewStylePlain);

self.delegate = self;
self.dataSource = self;

// UIBubbleTableView default properties

self.snapInterval = 120;

}

- (id)init
{
self = [super init];
if (self) [self initializator];
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) [self initializator];
return self;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) [self initializator];
return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:UITableViewStylePlain];
if (self) [self initializator];
return self;
}

#pragma mark - Override

- (void)reloadData
{
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
NSLog(@"%@",bubbleSection);
// Cleaning up
self.bubbleSection = nil;

// Loading new data
int count = 0;
self.bubbleSection = [[NSMutableArray alloc] init];

if (self.bubbleDataSource && (count = [self.bubbleDataSource rowsForBubbleTable:self]) > 0)
{
    NSMutableArray *bubbleData = [[NSMutableArray alloc] initWithCapacity:count];

    for (int i = 0; i < count; i++)
    {
        NSObject *object = [self.bubbleDataSource bubbleTableView:self dataForRow:i];
        assert([object isKindOfClass:[NSBubble class]]);
        [bubbleData addObject:object];

        [bubbleData sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
         {
             NSBubble *bubbleData1 = (NSBubble *)obj1;
             NSBubble *bubbleData2 = (NSBubble *)obj2;
             return [bubbleData1.datee compare:bubbleData2.datee];
         }];

        NSDate *last = [NSDate dateWithTimeIntervalSince1970:0];
        NSMutableArray *currentSection = nil;

        for (int i = 0; i < count; i++)
        {
            NSBubble *data = (NSBubble *)[bubbleData objectAtIndex:i];

            if ([data.datee timeIntervalSinceDate:last] > self.snapInterval)
            {
                currentSection = [[NSMutableArray alloc] init];
                [self.bubbleSection addObject:currentSection];
            }
            [currentSection addObject:data];
            last = data.datee;
        }
    }


}
 [super reloadData];
}
#pragma mark - UITableViewDataSource implementation


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int result = [self.bubbleSection count];

return result;

}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {


 return [[self.bubbleSection objectAtIndex:section] count];


 }

 - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {

 // Header
 if (indexPath.row == 0)
 {
     return [NSBubbleTblHeaderCell height];
 }

 NSBubble *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row - 1];
 return data.insetss.top + data.vieww.frame.size.height + data.insetss.bottom;

 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

 // Header with date and time
 if (indexPath.row == 0)
 {
     static NSString *cellId = @"tblBubbleHeaderCell";
     NSBubbleTblHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
     NSBubble *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:0];

     if (cell == nil) cell = [[NSBubbleTblHeaderCell alloc] init];

     cell.date = data.datee;

     return cell;
 }

 // Standard bubble
 static NSString *cellId = @"tblBubbleCell";
 NSBubbleTblCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
 NSBubble  *data = [[self.bubbleSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row - 1];

 if (cell == nil)

 cell = [[NSBubbleTblCell alloc] init];

 cell.data = data;

 return cell;

 }

在运行项目时显示多个。它似乎是出乎意料的。帮助将是必须赞赏的。他们可能是tableview及其数据源的一些问题我认为我理解我如何用这个url实现这个url有人帮助。

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要导入<UIKit/UIKit.h>。它添加了必要的UIImage,UITableView,UIView等。

答案 1 :(得分:0)

> ***Assertion failure in -[NSBubbleTblView
> _createPreparedCellForGlobalRow:withIndexPath:willDisplay:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:9269 2015-07-27
> 16:53:23.807 Hansel[6740:143046] *** Terminating app due to uncaught
> exception 'NSInternalInconsistencyException', reason: 'UITableView
> dataSource is not set'

正如您所看到的,此错误: UITableView数据源未设置表示您未设置表视图数据源。您必须在接口构建器中将表视图与数据源连接。