I am trying to create simple messaging app just like the built one.
I want to reproduce the same effect speech bubbles as iMessage.
I found a project from apple called MultipeerGroupChat which has that functionality.
The problem is It has a lot more than what I need making it hard to replicate, because of class dependencies. I dont need multipeer or sending images.I stripped a lot of code out already.
I now have a simple TableView, I added the bubble images and 2 classes:
MessageView.h
Transcript.h
I narrowed down the issue to this table view delegate to display the bubbles:
// The individual cells depend on the type of Transcript at a given row. We have 3 row types (i.e. 3 custom cells) for text string messages, resource transfer progress, and completed image resources
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get the transcript for this row
Transcript *transcript = [self.transcripts objectAtIndex:indexPath.row];
// Check if it's an image progress, completed image, or text message
UITableViewCell *cell;
if (nil != transcript.imageUrl) {
// It's a completed image
cell = [tableView dequeueReusableCellWithIdentifier:#"Image Cell" forIndexPath:indexPath];
// Get the image view
ImageView *imageView = (ImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
// Set up the image view for this transcript
imageView.transcript = transcript;
}
else if (nil != transcript.progress) {
// It's a resource transfer in progress
cell = [tableView dequeueReusableCellWithIdentifier:#"Progress Cell" forIndexPath:indexPath];
ProgressView *progressView = (ProgressView *)[cell viewWithTag:PROGRESS_VIEW_TAG];
// Set up the progress view for this transcript
progressView.transcript = transcript;
}
else {
// Get the associated cell type for messages
cell = [tableView dequeueReusableCellWithIdentifier:#"Message Cell" forIndexPath:indexPath];
// Get the message view
MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
// Set up the message view for this transcript
messageView.transcript = transcript;
}
return cell;
}
As mention before I only need the message so I stripped down to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Transcript *transcript = [self.messageArray objectAtIndex :[indexPath row]];
cell = [tableView dequeueReusableCellWithIdentifier:#"Message Cell" forIndexPath:indexPath];
MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
messageView.transcript = transcript;
//how does the code add the view and return it ?? :-S
return cell;
}
This code does not display anything.
Now I dont understand how this code customize the cell to show speech bubbles.
Please advice.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath on UITableView will return an instance of UITableViewCell that is ready to be reused or if no reusable cells exist it will create one.
However, according to the documentation, you need to first register a nib or Class with the table view so it knows what cell maps to that reuse identifier.
Check out the methods:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
You are missing:
[cell.contentView addSubview:messageView];
in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It should be:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Transcript *transcript = [self.messageArray objectAtIndex :[indexPath row]];
cell = [tableView dequeueReusableCellWithIdentifier:#"Message Cell" forIndexPath:indexPath];
MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
messageView.transcript = transcript;
[cell.contentView addSubview:messageView];
return cell;
}
Related
I have added tableview on top of UIViewController and I have also implemented the UITableViewDataSource and UITableViewDelegate methods
For some reason, I am not getting the value out of didSelectRowAtIndexPath. I get the expected value of indexPath.row but the selectedCell.textLabel.text returns nil.
I am not able to figure out what may be the problem. I am using the dynamic table.
//Array of multiple objects is filled through the delegated arrays and the arrays are properly filled.
#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 _arrayForMultipleObjects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
array = [[NSMutableArray alloc]init];
// Configure the cell...
_singleSelectionLabel= (UILabel *) [cell viewWithTag:2];
_singleSelectionLabel.text=[self.arrayForMultipleObjects objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *valu ;
NSLog(#"%i count",indexPath.row);
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
//Here cellText is null
if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
{ [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
valu = cellText;
}
NSLog(#"%#",valu);
}
If you are using storyboard then the default cell style of table cells is Custom which gives you a blank cell where you can add other types of controls to it.
If you dragged a label onto the cell and changed its text, then you are likely using a Custom style of UITableViewCell.
The property textLabel is only available on cells of types other than Custom, such as Basic, or Detailed.
Please double check if this is the case for you.
Edit: As ready suggested you can fetch the text like this:
self.arrayForMultipleObjects[indexPath.row]
I have a table view with a custom protoype cell and the cell has 3 different labels on it. How do I get access to these labels? I need to change their texts. I've searched around everywhere and haven't found something that helps me in this case. I have the below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
//change cell's 3 labels' text here
return cell;
}
How can I access the above cell's 3 different labels that it has?
You need to make a class that is a subclass of UITableViewCell, and set the custom cell's class to the class you made. Then you want to link the labels using IBOutlets in the cell to the subclass you made.
Finally, in -tableView:cellForRowAtIndexPath:, you should cast the cell to your new subclass so you get access to those IBOutlets. Assuming your outlets are named someLabelOutlet1, someLabelOutlet2, and someLabelOutlet3, do the following after you finished subclassing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
cell.someLabelOutlet1.text = #"sometext"
cell.someLabelOutlet2.text = #"sometext"
cell.someLabelOutlet3.text = #"sometext"
return cell;
}
In cellForRowAtIndexPath, you can access these labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"CustomCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
// set common properties here
cell.lbl1.textColor=[UIColor whiteColor];
cell.lbl1.backgroundColor=[UIColor redColor];
cell.lbl1.font=[UIFont fontWithName:#"Verdana" size:16.0];
}
cell.lbl1.text=#"lbl1Txt";
cell.lbl2.text=#"lbl2Txt";
cell.lbl3.text=#"lbl3Txt";
return cell;
}
I feed a UITableView with a list of names and images from a JSON. "SDWebImage" handles images download. It works OK apart from the fact that the images move to the left when the user selects a row or when scrolls the table view.
Two screen captures to show the issue.
Interface Builder setup
Implementation is pretty standard:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil ) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:#"marca"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row]objectForKey:#"photoUrl"]]placeholderImage:[UIImage imageNamed:#"placeholder.png"] ];
return cell;
}
What can I do to stop images moving?
I've just had a similar issue on an app and found it was because I was re-using UITableViewCells default imageView IBOutlet.
After creating my a new IBOutlet, called something other than imageView, and hooking it up it resolved the issue.
I used the following code on a UITableViewController.
- (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 [buyer 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];
}
buyerobject = [buyer objectAtIndex:indexPath.row];
UILabel *Label = (UILabel *)[cell viewWithTag:10];
NSString *temp = [[NSString alloc]initWithString:[buyerobject objectForKey:#"text"]];
Label.text = temp;
return cell;
}
When I print the above "temp" I am getting the output. But nothing is shown in the label inside the cell.
All the cell identifier and tag value are correctly given.
If someone had a similar problem and found the solution for this kind of errors then please help out.
This happens because the UILabel does not refer to an actual view in the UITableViewCell view hierarchy (you did not add it as a subview of the UITableViewCell). Try to set the label with cell with
[cell.textLabel setText:temp];
Can you try debugging with a breakpoint where you set the text and print out (po Label) what the label object is?
Based on your code you should use cell.textLabel.text=temp;.
You're also searching for a view with tag 10 inside the cell, but probably you've never set it.
I'm working on my first app and I'm kind of new to Objective-C, I want to make it so when someone types in a text field then presses a button it will save it to a table view. Does anyone know how to do this or know any tutorials.
all you have to do is everytime you press the button you'll refresh/update your tableview.
- (IBAction)buttonPressed:(id)sender {
NSString *textNameToAdd = yourTextField.text;
[containerArrayForTableView addObject:textNameToAdd];
[myTableView reloadData];
}
// UITABLEVIEW DELEGATES
- (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.
// Usually the number of items in your array (the one that holds your list)
return [containerArrayForTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell... setting the text of our cell's label
cell.textLabel.text = [containerArrayForTableView objectAtIndex:indexPath.row];
return cell;
}
refer here if you are having trouble configuring with UITableView.
These are some good tutorials for you..
http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/
http://iosmadesimple.blogspot.in/2012/10/adding-uitableview-tutorial.html
Happy coding.
EDIT:
Here what i made for you:
Simple Demo With TextField & TableView
EDIT 2:
Simple Demo With 2 TextFields & 2 TableViews