我正在制作一个新的实时聊天应用程序,我创建了一个包含聊天室的UIViewController,当有人点击一个单元格时,它打开了我创建的UIViewController,我编写了一个代码,但它没有做任何事情只是打开UIViewController没有任何反应!
这是chatTableView的代码
@implementation DMChatViewController
//---add this---
@synthesize chatTableView;
NSMutableArray *listOfMessages;
NSMutableArray *dateOfMessages;
static CGFloat const FONTSIZE = 14.0;
static int const DATELABEL_TAG = 1;
static int const MESSAGELABEL_TAG = 2;
static int const IMAGEVIEW_TAG_1 = 3;
static int const IMAGEVIEW_TAG_2 = 4;
static int const IMAGEVIEW_TAG_3 = 5;
static int const IMAGEVIEW_TAG_4 = 6;
static int const IMAGEVIEW_TAG_5 = 7;
static int const IMAGEVIEW_TAG_6 = 8;
static int const IMAGEVIEW_TAG_7 = 9;
static int const IMAGEVIEW_TAG_8 = 10;
static int const IMAGEVIEW_TAG_9 = 11;
int bubbleFragment_width, bubbleFragment_height;
int bubble_x, bubble_y;
//--------------
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
bubble_x = 10;
bubble_y = 20;
//---size of the bubble fragment---
bubbleFragment_width = 56;
bubbleFragment_height = 32;
//---contains the messages---
listOfMessages = [[NSMutableArray alloc] init];
//---contains the date for each message---
dateOfMessages = [[NSMutableArray alloc] init];
//---add a message---
[listOfMessages addObject:@"Hello!"];
[dateOfMessages addObject:[NSString stringWithFormat:@"%@",[NSDate date]]];
//--------------
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)chatTableView
{
return 1;
}
- (NSInteger)chatTableView:(UITableView *)chatTableView numberOfRowsInSection: (NSInteger)section
{
return [listOfMessages count];
}
//---calculate the height for the message---
-(CGFloat) labelHeight:(NSString *) text
{
CGSize maximumLabelSize = CGSizeMake((bubbleFragment_width * 3) - 25,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize: FONTSIZE] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
/** CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize: FONTSIZE] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; **/
// CGSize expectedLabelSize = [text sizeWithAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize: FONTSIZE]} /** constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping **/];
return expectedLabelSize.height;
}
//---returns the height for the table view row---
- (CGFloat)chatTableView:(UITableView *)chatTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int labelHeight = [self labelHeight:[listOfMessages objectAtIndex:indexPath.row]];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
return (bubble_y + bubbleFragment_height * 2 + labelHeight) + 5;
}
- (UITableViewCell *)chatTableView:(UITableView *)chatTableCell cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//---add this---
UILabel* dateLabel = nil;
UILabel* messageLabel = nil;
UIImageView *imageView_top_left = nil;
UIImageView *imageView_top_middle = nil;
UIImageView *imageView_top_right = nil;
UIImageView *imageView_middle_left = nil;
UIImageView *imageView_middle_right = nil;
UIImageView *imageView_middle_middle = nil;
UIImageView *imageView_bottom_left = nil;
UIImageView *imageView_bottom_middle = nil;
UIImageView *imageView_bottom_right = nil;
//--------------
UITableViewCell *cell = [chatTableCell dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//---add this---
//---date---
dateLabel = [[UILabel alloc] init];
dateLabel.tag = DATELABEL_TAG;
[cell.contentView addSubview: dateLabel];
//---top left---
imageView_top_left = [[UIImageView alloc] init];
imageView_top_left.tag = IMAGEVIEW_TAG_1;
[cell.contentView addSubview: imageView_top_left];
//---top middle---
imageView_top_middle = [[UIImageView alloc] init];
imageView_top_middle.tag = IMAGEVIEW_TAG_2;
[cell.contentView addSubview: imageView_top_middle];
//---top right---
imageView_top_right = [[UIImageView alloc] init];
imageView_top_right.tag = IMAGEVIEW_TAG_3;
[cell.contentView addSubview: imageView_top_right];
//---middle left---
imageView_middle_left = [[UIImageView alloc] init];
imageView_middle_left.tag = IMAGEVIEW_TAG_4;
[cell.contentView addSubview: imageView_middle_left];
//---middle middle---
imageView_middle_middle = [[UIImageView alloc] init];
imageView_middle_middle.tag = IMAGEVIEW_TAG_5;
[cell.contentView addSubview: imageView_middle_middle];
//---middle right---
imageView_middle_right = [[UIImageView alloc] init];
imageView_middle_right.tag = IMAGEVIEW_TAG_6;
[cell.contentView addSubview: imageView_middle_right];
//---bottom left---
imageView_bottom_left = [[UIImageView alloc] init];
imageView_bottom_left.tag = IMAGEVIEW_TAG_7;
[cell.contentView addSubview: imageView_bottom_left];
//---bottom middle---
imageView_bottom_middle = [[UIImageView alloc] init];
imageView_bottom_middle.tag = IMAGEVIEW_TAG_8;
[cell.contentView addSubview: imageView_bottom_middle];
//---bottom right---
imageView_bottom_right = [[UIImageView alloc] init];
imageView_bottom_right.tag = IMAGEVIEW_TAG_9;
[cell.contentView addSubview: imageView_bottom_right];
//---message---
messageLabel = [[UILabel alloc] init];
messageLabel.tag = MESSAGELABEL_TAG;
[cell.contentView addSubview: messageLabel];
//---set the images to display for each UIImageView---
imageView_top_left.image =
[UIImage imageNamed:@"bubble_top_left.png"];
imageView_top_middle.image =
[UIImage imageNamed:@"bubble_top_middle.png"];
imageView_top_right.image =
[UIImage imageNamed:@"bubble_top_right.png"];
imageView_middle_left.image =
[UIImage imageNamed:@"bubble_middle_left.png"];
imageView_middle_middle.image =
[UIImage imageNamed:@"bubble_middle_middle.png"];
imageView_middle_right.image =
[UIImage imageNamed:@"bubble_middle_right.png"];
imageView_bottom_left.image =
[UIImage imageNamed:@"bubble_bottom_left.png"];
imageView_bottom_middle.image =
[UIImage imageNamed:@"bubble_bottom_middle.png"];
imageView_bottom_right.image =
[UIImage imageNamed:@"bubble_bottom_right.png"];
} else {
//---reuse the old views---
dateLabel = (UILabel*)[cell.contentView viewWithTag: DATELABEL_TAG];
messageLabel = (UILabel*)[cell.contentView viewWithTag: MESSAGELABEL_TAG];
imageView_top_left =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_1];
imageView_top_middle =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_2];
imageView_top_right =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_3];
imageView_middle_left =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_4];
imageView_middle_middle =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_5];
imageView_middle_right =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_6];
imageView_bottom_left =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_7];
imageView_bottom_middle =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_8];
imageView_bottom_right =
(UIImageView*)[cell.contentView viewWithTag: IMAGEVIEW_TAG_9];
}
//---calculate the height for the label---
int labelHeight = [self labelHeight:[listOfMessages objectAtIndex:indexPath.row]];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
//---you can customize the look and feel for the date for each message here---
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = NSTextAlignmentCenter; // edited
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
/**
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor]; **/
//---top left---
imageView_top_left.frame =
CGRectMake(bubble_x, bubble_y, bubbleFragment_width, bubbleFragment_height);
//---top middle---
imageView_top_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---top right---
imageView_top_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2), bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---middle left---
imageView_middle_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle middle---
imageView_middle_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle right---
imageView_middle_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---bottom left---
imageView_bottom_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---bottom middle---
imageView_bottom_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width,
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height);
//---bottom right---
imageView_bottom_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---you can customize the look and feel for each message here---
messageLabel.frame =
CGRectMake(bubble_x + 10, bubble_y + 5,
(bubbleFragment_width * 3) - 25,
(bubbleFragment_height * 2) + labelHeight - 10);
messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.textColor = [UIColor darkTextColor];
messageLabel.numberOfLines = 0; //---display multiple lines---
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
dateLabel.text = [dateOfMessages objectAtIndex:indexPath.row];
messageLabel.text = [listOfMessages objectAtIndex:indexPath.row];
//--------------
return cell;
}
/**
//---add a message to the table view---
- (IBAction)btnAdd:(id) sender
{
[listOfMessages addObject:itemName.text];
[dateOfMessages addObject:[NSString stringWithFormat:@"%@",[NSDate date]]];
[self.tableView reloadData];
}
**/
@end
答案 0 :(得分:1)
如果您正在尝试提供数据源&amp;委托UITableView的方法,你已经错误地命名了你的方法。例如,
- (NSInteger)chatTableView:numberOfRowsInSection:
应该是
- (NSInteger)tableView:numberOfRowsInSection:
答案 1 :(得分:0)
您是否期望UITableViewDelegate调用
chatTableView:(UITableView *)chatTableCell cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法给你?
如果你是,你不知道代表是如何工作的。将该方法更改为:
tableView:(UITableView *)chatTableCell cellForRowAtIndexPath:(NSIndexPath *)indexPath