How to add different Custom Cells in one TableView with Storyboard? - xcode

i want to add 2 or more diffrent Custom cells in one Tableview, by using storyboard.
i know how to add diffrent cells without storyboard. I always do this by this way:
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//pictureCell = [[DetailPictureCell alloc]init];//(DetailPictureCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
pictureCell.header = true;
[pictureCell setHeader];
if (cell == nil) {
if ([indexPath row] == 0) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HeaderAngebotViewCell" owner:self options:nil];
NSLog(#"New Header Cell");
}
if([indexPath row] ==1){
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"productCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
And now my question: How i can do this with storyboard?
To add one custom cell is possible. But i can not add two diffrent cell. Can you help me please?

In the attributes inspector for the table view, select "Dynamic Prototypes" and below that select the number of prototype cells. Give each cell a different identifier and when dequeuing cells in cellForRowAtIndexPath, use the appropriate identifier based on indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row == 0) {
identifier = #"OneCellId";
} else if (indexPath.row == 1) {
identifier = #"OtherCellId";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//configure cell...
}

Related

How to change subview's title from a table selected row in Xcode?

Here is the problem: I have a table view, custom cells, and detail view. I have 10 rows in my table and when the user selects one of the rows, the detail view opens. How to set the detail view nav bar's title from the selected row of my table view? I have three classes - televisionList (my tableview), televisionCell (my cells in the table view) and televisionDetail (my detail view which opens when a row is selected). In my televisionCell I have declared the following label:
#property (weak, nonatomic) IBOutlet UILabel *tvLabel;
In my televisionList (the tableview) I have this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
televisionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"televisionCell" owner:self options:nil];
for (id currentObject in objects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (televisionCell *)currentObject;
break;
}
}
}
switch (indexPath.row)
{
case 0:
{
cell.imageView.image = bnt;
cell.tvLabel.text = #"БНТ 1";
cell.backgroundView.image = [UIImage imageNamed:#"lightbackgr.png"];
break;
}
case 1:
{
cell.imageView.image = btv;
cell.tvLabel.text = #"bTV";
cell.backgroundView.image = [UIImage imageNamed:#"background-cell.png"];
break;
}
case 2:
{
cell.imageView.image = novatv;
cell.tvLabel.text = #"Нова TV";
cell.backgroundView.image = [UIImage imageNamed:#"lightbackgr.png"];
break;
}
}
Now, I want to change my detail view title to be equal to cell.tvLabel.text when a row is selected. Where and how to do that? Thanks in advance!
As you wrote, you want to set the title, when the cell is selected, so the best way will be to use:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
televisionCell *cell=[tableView cellForRowAtIndexPath:indexPath];
details.title=cell.tvLabel.text;
}
However, you may consider introducing the model object to your app and keep them in some array(objects in the example below`. So the method would look like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourObject* obj=[self.objects objectAtIndex:indexPath.row];
details.title=obj.stationName;
}

Xcode: Why does my UITextField act slow in my UITableViewCell?

I have an app where I load a UIViewController with an UITableView. In the UITableViewCells I have a UITextField. Which I have added in the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
// Setting the cell textLabel.
[[cell textLabel] setText:[Customize objectAtIndex:indexPath.row]];
if ([indexPath row] == 0) { // Text
// Adding and customizing UITextField.
TextFieldText = [[UITextField alloc] initWithFrame:CGRectMake(160, 10, 140, 30)];
TextFieldText.placeholder = #"Random";
TextFieldText.autocorrectionType = UITextAutocorrectionTypeNo;
TextFieldText.autocapitalizationType = UITextAutocapitalizationTypeNone;
TextFieldText.returnKeyType = UIReturnKeyDone;
[cell addSubview:TextFieldText];
and so on.
The problem is that when you type in it, it is a little slow. When a letter/symbol is typed it appears in the UITextField and for a very short second the marker is still on the left side of the letter/symbol and slide across the letter/symbol. The keyboard also appears kind of slow.
Any thoughts?
Thanks in advance :)

Issue with UITableView - updating table values

I am a newbie. I have written some code for UITable but I am unable to update table values after adding the values to the array. I'm using a tableview subclass. The code is as follows. Check the last function. Now how can I update my table values?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_phones count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
HLPFoundPhones *p = (HLPFoundPhones *)[_phones objectAtIndex:indexPath.row];
NSString *subtitle = [NSString stringWithFormat:#"Found (%1.2f,%1.2f) on %#" ,[p loc].x, [p loc].y , [p foundDate]];
cell.textLabel.text = [p name];
cell.detailTextLabel.text = subtitle;
return cell;
}
- (void)insertNewObject
{
HLPPhonesAdd *view = [[HLPPhonesAdd alloc]initWithNibName:#"HLPPhonesAdd" bundle:nil];
[self.navigationController pushViewController:view animated:YES];
[view release];
}
- (void)updateTable:(CGPoint)loc name:(NSString *)_name{
HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
[_phones addObject:a];
[phones reloadData];
}
call
[yourTableView reloadData];
in your updateTable Function..
- (void)updateTable:(CGPoint)loc name:(NSString *)_name{
HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
[_phones addObject:a];
//give your tableView object instead of yourTableView
[yourTableView reloadData];
}
check value of _phone count and use in function
- (void)updateTable:(CGPoint)loc name:(NSString *)_name
{
HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
[_phones addObject:a];
[table_name reloaddata];
}
Confirm the array _phones is allocated.
Then there will be problems if the [tableview reloadData] is called inside a thread.
So confirm the reloadData is called from the main thread.
First of all, your cell identifier looks to be same for all cells, which may confuse iOS which cell needs to be dequeued. So, keep your cell identifiers unique, something like below :
NSString *cellIdentifier = [NSString stringWithFormat:#"Cell-%d", indexPath.row];
then, after table view is first displayed, update your datasource and call:
[tableView reloadData];

xcode uitableview cell image align

I'm sure this is very simple but i can't find any documentation about it..
in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl)
i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"tableicon.png"];
NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:#"ArticleTitle"];
cell.textLabel.text = cellValue;
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.numberOfLines = 2;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.
UIImageView *pic = myImageView;
UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
picContainer.contentMode = UIViewContentModeRight;
pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
[picContainer addSubview:pic];
[cell.contentView addSubview:picContainer];
This has the added benefit of automatically readjusting when the orientation changes.
try this one
cell.accessoryView=[UIImage imageNamed:#"tableicon.png"];

all images in table view are the same in iPhone app

I have a table view where I want a different image for each cell based on logic. Here is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier] autorelease];
}
NSInteger *row = [indexPath row];
NSString *platform = [[myStringArray objectAtIndex: row];
cell.imageView.image = [self imageForInventory:platform];
return cell;
}
Here is my imageForInventory method:
- (UIImage *)imageForInventory:(NSString *)platform {
if (platform = #"Win") {
UIImage *winImage = [UIImage imageNamed:#"Vista.png"];
return winImage;
}else if (platform = #"Mac") {
UIImage *appleImage = [UIImage imageNamed:#"Apple.png"];
return appleImage;
}else if (platform = #"Linux") {
UIImage *linuxImage = [UIImage imageNamed:#"Linux.png"];
return linuxImage;
}
return nil;
}
This shows the winImage for every row ignoring the if statements. How do I get the image to set accordingly per cell?
It's probably your comparison code, to compare strings in imageForInventory use [str1 isEqualToString:str2]. You're using = which is the assignment operator right now, but even if you change it to == it wouldn't work.

Resources