how to add custom uitableviewcell to uitableview created with ib? - iphone

I have uitableview created with IB. And I designed my own uitableviewcell (it is subclass of uiatbleviewcell and has its own xib file). How to add it to my uitableview??
I tried
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = #"myCell";
myCell *cell = (myCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"myCell" owner:self options:nil];
cell = [xib objectAtIndex:0];
}
// Configure the cell...
cell.title.text = #"tableCell";
cell.preview.text = #"preview";
cell.postedTime.text = #"right now";
return cell;
}
It works incorrect. I can't include images here, because my rep is 10 (need min. 11 to upload images). So here's link to show my bad results. This is what i want: http://img708.imageshack.us/img708/5082/20120903153930.png
This is what I have:
http://img836.imageshack.us/img836/5844/20120903154405.png

u need to change the default height of each cell using method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62.0f; // enter value if u know or test
}
hope it helps. happy coding :)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(btnTag == 1)
return [array1 count];
if(btnTag == 2)
return [array2 count];
return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(btnTag == 1)
{
cell.teamLabel.text = #"i am 'A'cell";
}
if(btnTag == 2)
{
cell.teamLabel.text = #"i am 'B'cell";
}
return cell;
}

You dont have to add it like this on your xib file. Acc to what you have done you have added a UITableViewCell on another UITableView. Do you really want to create a custom cell ?? You can simply add any data into the UITableView in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then add the cell's accordingly like UILabels , UIImageViews etc .
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil ];
}
UIButton *Imagebutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 30, 30)];
UIImage *img = [UIImage imageNamed:#"Lock Unlock.png"];
[Imagebutton setImage:img forState:UIControlStateNormal];
[Imagebutton addTarget:self action:#selector(aMethod1:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:Imagebutton];
UILabel *MealsforLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 140, 30)];
MealsforLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:MealsforLabel];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
Something like the above.

Related

How to set a color for a Cell on a TableView?

I want the whole cell printed in blue but it only shows a little band.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
My screen capture is this:
add this line after [cell.contentView setBackgroundColor:[UIColor blueColor]]; line:
cell.textLabel.backgroundColor = [UIColor clearColor];
Because of the way UITableView changes a cell's background color during selection, you must implement tableView:willDisplayCell:forRowAtIndexPath: and set the cell background there. You should set the background of the whole cell rather than just its contentView, otherwise the accessory views will not be highlighted.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
cell.backgroundColor = ...;
}
You may also need to make the various subviews in the cell have transparent background colors.
cell.titleLabel.backgroundColor = [UIColor clearColor];
cell.titleLabel.opaque = NO;
Use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
Answer : tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor blueColor];
}

Dispalying text and label in custom cell of UItableView

Hi all I've custom cells in which I've UIImageView and UILabel I want to assign Image to UIImageView and text to UILabel which are in array, please can any one help me?? thanks in advance
I tried below code
- (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.textLabel.text =[CurArray objectAtIndex:indexPath.row];
NSString *cellIcon = [[self IconArray] objectAtIndex:indexPath.row];
UIImage *cellIconimage = [UIImage imageNamed:cellIcon];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cellIconimage];
[imageView setFrame:CGRectMake(100.0, 30.0, 30.0, 30.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
return cell;
}
With this code i can assign image to created imageview But I'm not able to to assign text for UILable
here IBCustomCellProjectList is an example which is UITableViewCell with XIB just follow the logic
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
IBCustomCellProjectList *cell = (IBCustomCellProjectList *) [tableView dequeueReusableCellWithIdentifier:nil];
// yourCustomeCell *cell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IBCustomCellProjectList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (IBCustomCellProjectList *) currentObject;
break;
}
}
}
// Configure the cell.
cell.cellLabel.text = [yourTextArray objectAtIndex:i];
cell.cellImageView.image = [UIImage imageNamed:[yourImageArray objectAtIndex:i]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also see this bellow tutorial..
Custom cell with advance control and design
http://www.appcoda.com/customize-table-view-cells-for-uitableview/Customize tableview Cell
:)
Use this code. Maybe this is what you want. You will have to use the method cellForRowAtIndexPath .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.Label.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.Label.textColor = [UIColor blackColor];
cell.ImageView.image = [UIImage imageNamed:[CountryFlag objectAtIndex:indexPath.row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Use Following code:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
const NSInteger IMAGE_VIEW_TAG=1001;
const NSInteger LABEL_TAG=1002;
UIImageView *imageView;
UILabel *lbl;
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)] autorelease];
imageView.tag=IMAGE_VIEW_TAG;
imageView.contentMode=UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 20, 160, 20)];
lbl.font=[UIFont fontWithName:#"Helvetica" size:14.0];
lbl.adjustsFontSizeToFitWidth=YES;
lbl.minimumFontSize=10.0;
lbl.textAlignment=UITextAlignmentLeft;
lbl.textColor=[UIColor colorWithRed:73.0/255.0 green:73.0/255.0 blue:73.0/255.0 alpha:1.0];
lbl.backgroundColor=[UIColor clearColor];
lbl.tag=LABEL_TAG;
[cell.contentView addSubview:lbl];
}
imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
lbl=(UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
imageView.image=[UIImage imageNamed:#"image.png"];
lbl.text=#"Your Text";
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65.0;
}

Compare indexpath Of Cell with uiButton Is This Possible?

I have a Uibutton in one class and Uitable in different Class...
Now i want to compare IndexPAth(Particular cell Position) with Uibutton Of Different class...
Actually i want to print Different different value on particular cell Which I am Fetching from Different Different Uibutton`s Methods from another class....
Here is My code....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//- (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];
}
if(indexPath == btn1){ // This is the Line What i want , i am trying this but dont go inside the condition..
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 290, 60)];
lbl.backgroundColor = [UIColor clearColor];
//lbl.editable = NO;
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
// txtView1.text= textviewstring;
// txtView1.text = #"Personal";
if([fetchFormArray222 count]<1){
lbl.text=#"";
}
else{
lbl.text = [fetchFormArray222 objectAtIndex:0];
}
// [txtView1 setDelegate:self];
lbl.textColor = [UIColor blackColor];
lbl.textAlignment = UITextAlignmentLeft;
[[cell contentView] addSubview:lbl];
}
else {
}
Check the apple documentation of UITableView.
Have you tried to use the button in the other class to perform a new fetch in the class you're focusing on that loads fetctFormArray222 and then reload the UITableView?

Custom textfield in uitableviewcontroller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"Cell";
NSString *CellIdentifier = [NSStringstringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
Name.tag=kViewTag;
[cell.contentView addSubview:Name];
//cell.textLabel.text=#"First Name";
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 30)];
textName.placeholder=#"Enter First Name";
textName.tag=kViewTag;
[textName setBorderStyle:UITextBorderStyleRoundedRect];
//textName.tag=0;
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
textName.clearsOnBeginEditing=YES;
[cell.contentView addSubview:textName];
}
}
return cell;
}
in this code there are some more textfields also.
my problem is that when i am scrolling my table value in the textfield removes automatically..
can anyone help me???
To add a textField in a cell make your own UITableViewCell like this.
Update :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"YourCellId";
YourCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [YourCell cellFromNib];
}
cell.yourTextField.tag = indexPath.row;
cell.yourTextField.text = (NSString*)[self.textFieldValues objectAtIndex:indexPath.row];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.textFieldValues replaceObjectAtIndex:textField.tag withObject:textField.text];
}

Why don't tablecells get released?

I simplified my code to test with, and still on the phone my memory usage keeps climbing to a point where the table slows way down.
Can someone tell me what I'm doing wrong here?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
}
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
Oops. That code paste didn't work too well. Here's a straight paste:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
}
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
return cell;
}
You will want to follow a pattern like this:
#define kTagMyLabel 1
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell1";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
UILabel * l;
if (cell == nil) {
// create the cell
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
// perform setup/functions that are common to all cells
l = [[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)] autorelease];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.tag = kTagMyLabel ;
[cell.contentView addSubview:l];
}
else
{
// find the label we previously added.
l = (UILabel*)[cell viewWithTag:kTagMyLabel];
}
// now set up the cell specific to this indexPath
l.text=#"Just some random text here";
return cell;
}
You're recycling UITableViewCell instances, but you're still creating a new UILabel instance for every row, and adding it to each and every cell. This is where the memory usage comes from. Try something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
}
return cell;
}
Since every UITableViewCell has its own standard UILabel (cell.textLabel), do you really need that extra label added to the contentView? If you need custom cells, you might want to consider subclassing UITableViewCell.