Some sort of text field - table view [closed] - ios

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm wondering how I can get something like the image above. How do you make this and does anybody got some sort of sample code?

you need to create a custom cell for your table, design your cell what ever you want. Put textView or any thing, and load your table with that custom cell.

Design your custom cell(use custom font for your labels to get that exact appearance if necessary.)
Custom cell design
And customize the Cell for row at index delegate method like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
//
// Configure the cell...
//Customiztion of cell
static NSString *cellIdentifier=#"cell";
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[SampleTableCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (SampleTableCell *) currentObject;
break;
}
}
}
cell.label1.text = #"Server";
cell.label2.text = #"vereist"
return cell;
}

It is called custom UITableViewCell, There are consist of Two Controls
1 UILabel
2 UITextField
Put this two control in cellForRowAtIndexPath with Specific fram size.
OR
Put This Whole code as it is.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 23) style:UITableViewStyleGrouped];
self.tblView.delegate = self;
self.tblView.dataSource = self;
self.tblView.backgroundView = nil;
self.tblView.backgroundColor = [UIColor clearColor];
self.tblView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tblView];
self.placeHolderValue = [[NSMutableArray alloc]initWithObjects:#"server ", #"account", #"Roof Coverings", #"Wall token", #"Roof", #"Floor", #"Guttering", nil];
self.textFields = [NSMutableArray array];
self.textLabels = [NSMutableArray array];
for (int i = 0; i < self.placeHolderValue.count; i++)
{
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(155.0, 0.0, 150.0, 44.0)];
textFiled.placeholder = #"Vereist";
textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textFiled.autocorrectionType = UITextAutocorrectionTypeNo;
textFiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
textFiled.borderStyle = UITextBorderStyleNone;
textFiled.clearButtonMode = UITextFieldViewModeAlways;
textFiled.delegate = self;
textFiled.tag = i+1;
textFiled.font = [UIFont systemFontOfSize:14];
if (self.listOfDetails.count > 0)
{
textFiled.text = [[self.listOfDetails objectAtIndex:0] objectForKey:[self.dbFieldName objectAtIndex:i]];
isEmpty = NO;
}
[self.textFields addObject:textFiled];
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 140, 44)];
lab.text = [self.placeHolderValue objectAtIndex:i];
lab.textAlignment = NSTextAlignmentLeft;
lab.numberOfLines = 0;
lab.font = [UIFont systemFontOfSize:14];
lab.backgroundColor = [UIColor clearColor];
[self.textLabels addObject:lab];
}
}
#pragma mark -
#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.placeHolderValue.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//static NSString *CellIdentifier = #"Cell";
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self.textLabels objectAtIndex:indexPath.row]];
[cell.contentView addSubview:[self.textFields objectAtIndex:indexPath.row]];
}
return cell;
}

Related

Add Storyboard UITableView and custom UITableViewCells to UIScrollView programmatically

In my Storboard, I have a UIViewController (ParentClass) -> UIScrollView (linked to Parent) -> UITableView (linked to Parent) -> UITableViewCell (2 cells, each with their own Class).
What I'm trying to do is add several instances of the UITableView (with the custom cells) into the UIScrollView.
I am close with the following code, but the cells are coming up empty:
self._scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self._scrollView.pagingEnabled = YES;
self._scrollView.showsVerticalScrollIndicator = NO;
self._scrollView.alwaysBounceVertical = NO;
NSInteger numberOfViews = 22;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UITableViewController *theTableView = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
theTableView.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
theTableView.tableView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self._scrollView.frame.size.height);
theTableView.tableView.backgroundColor = [UIColor colorWithRed:246/255.0 green:248/255.0 blue:249/255.0 alpha:1];
theTableView.tableView.delegate = self;
theTableView.tableView.dataSource = self;
theTableView.tableView.tag = 100+i;
[self._scrollView addSubview:theTableView.tableView];
}
self._scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:self._scrollView];
All the tables show up and all the pages are there and the scroll as they should, but the Custom Cell layouts are not showing up.
Here's the code I use to layout the tables:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) {
return 127.0;
} else {
return 56.0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"MealImageCell";
MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MealImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureImageCell:cell atIndexPath:indexPath];
return cell;
} else {
static NSString *CellIdentifier = #"MealInfoCell";
MealInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MealInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureInfoCell:cell atIndexPath:indexPath];
return cell;
}
}
- (void)configureImageCell:(MealImageCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *dictMeal = [[NSMutableDictionary alloc] init];
if ([arrayMeals count]) {
dictMeal = [arrayMeals objectAtIndex:indexPath.section];
cell.userInteractionEnabled = YES;
} else {
cell.userInteractionEnabled = NO;
}
[cell.imageFood sd_setImageWithURL:[NSURL URLWithString:[dictMeal valueForKey:#"img_src"]]
placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:#"meal%li.jpeg",(long) indexPath.section+1]]];
}
- (void)configureInfoCell:(MealInfoCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSString *mealName;
if ([arrayMeals count]) {
NSMutableDictionary *dictMeal = [arrayMeals objectAtIndex:indexPath.section];
cell.userInteractionEnabled = YES;
if ([dictMeal valueForKey:#"mealDescription"] == [NSNull null]) {
mealName = NSLocalizedString(#"No Name", nil);
} else if ([[dictMeal valueForKey:#"mealDescription"] isEqualToString:#""]) {
mealName = NSLocalizedString(#"Deleted", nil);
} else {
mealName = [decodeHTMLEntities decodeHTMLEntities:[[dictMeal valueForKey:#"mealDescription"] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
}
} else {
cell.userInteractionEnabled = NO;
mealName = NSLocalizedString(#"Loaing Meal...", nil);
}
cell.labelFood.text = mealName;
}
Without the scrollView, the table is populated as it should be.
The answer is to relace:
MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
with
MealImageCell *cell = [_myTablView dequeueReusableCellWithIdentifier:CellIdentifier];
with _myTableView being the IBOutlet for the table view in Storyboard.

How come my UITableView is repeating rows after 5 rows?

I have 7 rows in my database. I confirmed that all of the data is successfully coming over to the iOS side by NSLog the postArray which gives 7. However when I run my app, it will only display the first 5 rows, and then the first 2 rows instead of the 6th and 7th row from my database. Also when I NSLog the actual text from my 6th and 7th text view the correct text is in there... Why is it repeating after 5 rows? Thank you. Here is my code:
#import "DailyViewController.h"
#import "Post.h"
#interface DailyViewController ()
#end
#implementation DailyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// View background
[self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://example.org/getPosts.php"]];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
postArray = [[NSMutableArray alloc] init];
for(int i = 0; i < jsonArray.count; i++)
{
NSString *nickname = [[jsonArray objectAtIndex:i] objectForKey:#"nickname"];
NSString *squeal = [[jsonArray objectAtIndex:i] objectForKey:#"squeal"];
[postArray addObject:[[Post alloc] initWithNickname:nickname andSqueal:squeal]];
}
viewArray = [[NSMutableArray alloc] init];
for(int i = 0; i < postArray.count; i++)
{
Post *postObject;
postObject = [postArray objectAtIndex:i];
UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 15, 320, 30)];
nicknameLabel.text = postObject.nickname;
nicknameLabel.font = [UIFont boldSystemFontOfSize:20];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 42, 320, 0)];
textView.font = [UIFont systemFontOfSize:15];
textView.text = postObject.squeal;
textView.editable = false;
[textView setScrollEnabled:false];
[textView sizeToFit];
[textView layoutIfNeeded];
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textView.frame.size.height)];
[view addSubview:nicknameLabel];
[view addSubview:textView];
[viewArray addObject:view];
}
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return postArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [viewArray objectAtIndex:indexPath.row];
return view.frame.size.height+30;
}
#end
UITableView reuses the cells with the same reuse identifier, which means that when you scroll so that a row is scrolled out of the visible area, it will be used to show new rows. That's why when your 1st and 2nd rows were scrolled out of the view, they were used to show the 5th and 6th row.
You need to modify how you load cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Add and setup views here.
}
//Add content to view here
// e.g. cell.textLabel.text = #"Some text";
return cell;
}
BTW You can use the property textLabelof UITableViewCellinstead of creating a new label and adding it as as subview. If you need to have more control over your custom cell, then you should create a custom class and use a Storyboard file or a Xib file.
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
[cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
In your cellForRowAtIndexPath method, you are adding a subview to the cell's content view only if the cell is being initialized for the first time.
Rather, you should add/replace the subview on each call to cellForRowAtIndexPath.

Having issue using dequeueReusableCellWithIdentifier within cellForRowAtIndexPath

I'm using below code to add a text label into a static table with 4 sections and 3 row in each.
for some reason, when I first run the app its fine, but when I scroll up and down, the UItable doesn't show up properly. Can someone tell why?
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"%d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
return cell;
I don't think you need and else statement while building your cell, this is an edited code that may work out for you:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
// Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%d",indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}
/*else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"cell nr. %d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
*/
return cell;
}
I've commented your last lines of code so you won't lose them. If you also set the style of your TabelView as "Grouped", the result you'll get is the following:
http://s2.postimg.org/820thcmy1/i_OS_Simulator_Screen_shot_26_giu_2014_14_46_41.png
Hope this helps!

NSTextAligment applying only after editing textfield

I create table view cell which contains my custom text field, subclass of UITextField. When i add text field to my cell i set
textField.textAlignment = NSTextAlignmentCenter;
I have placeholder on text field. When my cell appears it's alignment is something average between left and center. Then after editing it becoming center. I need center alignment right after creating cell. Can anyone help to solve this?
Edit
My CellForRowAtIndexPath method:
- (CTKMultiColumnTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.section];
CTKMultiColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[CTKMultiColumnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
[cell setFrameWithRect:CGRectMake(0, 0, tableView.frame.size.width, 34)];
}
cell.textField.placeholder = [_placeholders objectAtIndex:tableView.tag];
cell.delegate = self;
cell.relatedTableView = tableView;
return cell;
}
and part of willDisplayCell method
- (void)tableView:(UITableView *)tableView willDisplayCell:(CTKMultiColumnTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (tableView.tag) {
case 4:
cell.textField.leftView = nil;
cell.textField.rightView = nil;
cell.textField.textAlignment = NSTextAlignmentCenter;
cell.textField.keyboardType = UIKeyboardTypeNumberPad;
cell.isPickable = NO;
cell.valueLimitMin = 0;
cell.valueLimitMax = 1000;
if (((CTKRoom *)[_roomsArray objectAtIndex:indexPath.section]).heatedArea) {
cell.textField.text = [NSString stringWithFormat:#"%#", ((CTKRoom *)[_roomsArray objectAtIndex:indexPath.section]).heatedArea];
}
else cell.textField.text = #"";
break;
}
this part is example of setting properties to my cell
I solved my problem. The reason was
myTextField.rightViewMode = UITextFieldViewModeAlways;
In my case right view was necessary, so in case when it wasn't needed i set
myTextField.rightView.frame = CGRectMake(0, 0, 1, 0);

tableview's uitableviewcell display in worng order and duplicate rows

This is a chat app, I'm using tableview to display the chatting data between user and others, however I'v got Rows are displayed multiple(duplicate). The code is below:
#define kMyTag 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
if ([[messageDic objectForKey:#"myself"]boolValue] == false) {
static NSString *CellIdentifier = #"MessageChatFriendCell";
MessageChatFriendCell *cell = (MessageChatFriendCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MessageChatFriendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
UIView *chatView = [chatInfo objectForKey:#"view"];
chatView.tag = kMyTag;
[cell.contentView addSubview:chatView];
return cell;
} else {
static NSString *CellIdentifier = #"MessageChatSelfCell";
MessageChatSelfCell *cell = (MessageChatSelfCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MessageChatSelfCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
UIView *chatView = [chatInfo objectForKey:#"view"];
chatView.tag = kMyTag;
[cell.contentView addSubview:chatView];
return cell;
}
}
the print results for each row are:
2012-08-02 15:44:47.152 MessageChatSelfCell-><UIView: 0x222790; frame = (85 0; 230 114); tag = 1; layer = <CALayer: 0x222320>>
2012-08-02 15:44:47.166 MessageChatFriendCell-><UIView: 0x21d790; frame = (0 0; 210 132); tag = 1; layer = <CALayer: 0x21d5c0>>
2012-08-02 15:44:47.176 MessageChatFriendCell-><UIView: 0x21bde0; frame = (0 0; 177 60); tag = 1; layer = <CALayer: 0x21be10>>
2012-08-02 15:44:47.183 MessageChatSelfCell-><UIView: 0x216430; frame = (85 0; 230 168); tag = 1; layer = <CALayer: 0x215fc0>>
2012-08-02 15:44:59.232 MessageChatFriendCell-><UIView: 0x215150; frame = (0 0; 86 60); tag = 1; layer = <CALayer: 0x214eb0>>
2012-08-02 15:45:00.465 MessageChatSelfCell-><UIView: 0x213220; frame = (85 0; 230 78); tag = 1; layer = <CALayer: 0x213250>>
Please advise, thanks!
Updated 2012-08-08
#define WRITER_NAME_LABEL_TAG 4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MessageChatSelfCellIdentifier = #"MessageChatSelfCell";
static NSString *MessageChatFriendCellIdentifier = #"MessageChatFriendCell";
UITableViewCell *cell = nil;
UIView *chatView = nil;
NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
NSString *text = [messageDic objectForKey:#"compiled"];
BOOL myMessage = [[messageDic objectForKey:#"myself"] boolValue];
if (myMessage) {
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatSelfCellIdentifier];
chatView = [self bubbleView:[NSString stringWithFormat:#"%#", text]
from:YES];
chatView.tag = WRITER_NAME_LABEL_TAG;
NSLog(#"MessageChatFriendCell->%#",chatView);
[cell addSubview:chatView];
}else {
chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatFriendCellIdentifier];
NSString *text = [messageDic objectForKey:#"compiled"];
UIView *chatView = [self bubbleView:[NSString stringWithFormat:#"%#", text]
from:NO];
chatView.tag = WRITER_NAME_LABEL_TAG;
NSLog(#"MessageChatSelfCell->%#",chatView);
[cell addSubview:chatView];
}else {
chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
return cell;
}
I answered your another question How to calculate the Width and Height of NSString on UILabel
They are similar problem. the code below I C&P from the other question.
That's because you did not reuse the table cell, the structure should be like:
NSString *text = [messageInfo objectForKey:#"compiled"];
if(cell == nil)
{
writerNameLabel.numberOfLines = 0;
writerNameLabel.textAlignment = UITextAlignmentRight;
writerNameLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];
I've been gone through and answered some of your question, that's correct way to write your tableview controller. And your problem will be solved.
Few things u should change, first don't store your views in chatInfo instead store your data.
Next add the view to the cell only when u create new cell, otherwise u will add more then one view to a reused cell.
Take a look at the example below, here I'm creating a cell with the message writer name, for my messages the writer name will be in the right hand side, and for my friend messages it will be on the left.
Pay attention that I'm only add the writer name label when I create a new cell, then I give it a tag so I can get the label next time I want to change it.
#define WRITER_NAME_LABEL_TAG 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MessageChatFriendCellIdentifier = #"MessageChatFriendCell";
static NSString *MessageChatSelfCellIdentifier = #"MessageChatSelfCell";
UITableViewCell *cell = nil;
UILabel *writerNameLabel = nil;
NSDictionary *messageInfo = [self.chatArray objectAtIndex:indexPath.row];
BOOL myMessage = [[messageInfo objectForKey:#"myself"] boolValue];
if (myMessage) {
// this is my message
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatSelfCellIdentifier] autorelease];
writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
writerNameLabel.textAlignment = UITextAlignmentRight;
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
else {
// this is my friend message
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatFriendCellIdentifier] autorelease];
writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
writerNameLabel.textAlignment = UITextAlignmentLeft;
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
// get the current writer name from the model
NSString *writerName = [messageInfo objectForKey:#"writer"];
// configure cell...
writerNameLabel.text = writerName;
return cell;
}

Resources