How to place a Rounded Rect UIBUTTON on UITableViewCell programmatically - iphone

I was trying to place two uibuttons on uitableviewcell programmatically.. but unable to achieve that>. somehow i managed to get the buttons through some sample project in which u can draw a nib for cell.. but i was unable to set a selector function for buttons.. in other example i can place a uiinfodark type button but when i change the type to rounded rect the button gets disappeared and i am unable to figure out why? I might be doing some silly mistake but I am really kinda stuck with it right now so I'll be very much thankful to all of you for helping in this regard.

You will need a UITableViewDataSource
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Then something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Get the object to display and set the value in the cell.
[cell addSubView: YOUR_BUTTON];
[cell addSubView: YOUR_OTHER_BUTTON];
return cell;
}

Related

Slider gets reload in Table View in Iphone SDK

In my app, i add a slider in Table view i.e. each row contains a slider & It also works fine.
But when i scroll the table view the slider get reload i.e. each shows me starting position rather than slider value.
//My code is as follow for slider in table cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier=[NSString stringWithFormat:#"CellIdentifier%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
return cell;
}
UISlider* theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
theSlider.maximumValue=99;
theSlider.minimumValue=0;
[cell addSubview:theSlider];
return cell;
}
How can i solve this??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier=[NSString stringWithFormat:#"CellIdentifier%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; UISlider* theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
theSlider.maximumValue=99;
theSlider.minimumValue=0;
[cell addSubview:theSlider];
} return cell;
This way the slider gets created only when cell is nil i.e cell gets created. and use tableView:willDisplayCell:forRowAtIndexPath: method to set the slider value like slider.value = yourvalue;
You need to store values of slider view and set the values of sliderview in cellForRowAtIndexPath slider.value = yourvalue;
The problem is that cellForRowAtIndexPath is called not just once, but every time that the tableView need to render that cell... So you have to be sure that only initialize theSlider the first time is called...
The best approach is to define a custom UITableViewCell and put there a property that would store theSlider, then, when cellForRowAtIndexPath is called, check if it is already initialized or not, see:
// If the slider is not yet initialized, then do it
if(cell.theSlider == nil)
{
// Init...
}
This is really from "cellForRowAtIndexPath" method . Every time when you are scrolling every cell is getting nil and reinitialise it. Better you can try with creating custom cell class(BY creating sub class of UITableViewCell) and also the defining slider in condition "if (cell == nil)".

Custom UITableViewCell does not show the labels in storyboard

In this screen shot you can see that I have added UITableView in UIViewController then customized the UITableViewCell by adding some labels in it. But the issue is when I run the application. All of the cells are empty. There are no labels at all.
I am not getting what can be the issue. I have searched the web and read tutorials but couldn't resolve it.
I resolved this issue by myself, just after little effort.
Actually when you create a custom cell you have to do these things:
Setup the labels, images etc on storyboard cell.
Create a custom cell class (inheriting from UITableViewCell)(CustomCell.h & .m), CustomCell.h having all of the properties as iboutlet for labels, images etc. and synthesize them all in implementation.
After creating this custom class go back to storyboard, select the custom cell, change its class to the CustomCell and give it some identifier like "MyCustomCell", then right click on the custom cell and connect the IBOutlets with labels etc.
Now import CustomCell class in the class where you are implementing the UITableView and use the properties you defined in CustomCell class.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
cell.myCustomLabel.text = #"My Text";
return cell;
}
I did this all and my issue was resolved and please don't forget to connect the delegates & datasource and table.
Hope this will help others.
It is little bit late but you can solve your problem by setting the background color of your view as Clear Color from your storyboard.
in the tableview's delegate method , cellforrowatindexpath has a uitableviewcell inside it , there should be an identifier in the initialization of this cell. possibly it is "cell" or "cellIdentifier" .
you just need to select your cell from storyboard and enter this identifier string to the storyboard , where uitableviewcell's attribute inspector stays.
hope this helps.. :)
First Set cell identifier in storyboard #"Cell"
then set tag of label
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel *lblDate = (UILabel*)[cell viewWithTag:101];
UILabel *lblDistance = (UILabel*)[cell viewWithTag:102];
UILabel *lbltype = (UILabel*)[cell viewWithTag:103];
lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:#"date"] stringByReplacingOccurrencesOfString:#"-" withString:#"/"]];
lblDistance.text = [NSString stringWithFormat:#"%# KM",[[temp objectAtIndex:indexPath.row] valueForKey:#"distance"]];
NSString *type = [NSString stringWithFormat:#"%#",[[temp objectAtIndex:indexPath.row] valueForKey:#"distance"]];
if([type isEqualToString:#"0"])
{
lbltype.text = #"Personal";
}
else
{
lbltype.text = #"Bussiness";
}
// Configure
return cell;
}

update uitableview cell object

i have a tableview that contains a uiwebview in its first row. i would like to change this tables' webview object with new one when user clicks a button. i am using the code given below but it does not work fine. older object is there and the newer one is over it although i recreate the webview. how can i remove the older one from cell?
thanks...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"NİL.......");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
NSLog(#"NOT NİL.......");
}
[cell addSubview:webView];
return cell;
}
Since you are using "dequeueReusableCellWithIdentifier", your should reconfigure the cell each time when the cell is displayed.
Please remember that the cells with same identity will be reused. It's better to set the same identity to one particular type of cells, typically cells with same subviews and layout.
This is sample for how to load data and reconfigure a cell: http://code.google.com/p/tweetero/source/browse/trunk/Classes/MessageListController.m
Here is a tutorial to get familiar with UITableView:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html

In-place editing of text in UITableViewCell?

I know this has to be a really simple question, but I am having trouble finding it in a simple place where there aren't so many other things going on that I can isolate the specific behavior.
I know I need to add a UITextField as a subview to the UITableViewCell to make it editable. What is the best way to do this? (I see an example in UICatalog, but I am confused with all of the extra things it is doing with the Dictionary.)
Again, please excuse the ignorance of this question. A pointer to sample code or a screencast would be much appreciated.
Thanks in advance,
Alan
First, define about UITextField.
txtField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
txtField.autoresizesSubviews=YES;
txtField.layer.cornerRadius=10.0;
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setPlaceholder:#"Type Data Here"];
Then write the code below in cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell addSubview:txtField];
return cell;
}
You can create your custom UITableViewCell and put whatever you want in it.
Then you simply have to use it in your UITableView.
If you are worried about what to use as UITextField delegate for your textfield to manage all the delegate methods, IMHO I'll put it in the UIViewController that handle the UITableView, so that you can update the DataSource once that the textfield has been edited.

How to display multiple columns in a UITableView?

I want to display multiple columns in a UITableView.
For Example:
TableView
FName LName Age
----- ----- ---
Abby Michale 34
I think the correct way of doing this is UICollectionView. Started with UITableView and finally failed at some point due to unexpected behavior while trying to implement scrolling both left-right and up-down.
Please check this awesome post for a complete and up-to-date solution:
http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns
Result will be like this:
You can define a custom cell in IB, which will contain 3 labels and in function:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = #"DetailCell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:#"DetailCell" owner:self options:nil];
cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
}
// setup your cell
}
when you define the cell in IB give each label a tag, so when you need to put a text there you can retrieve it by tag like this:
label = (UILabel *)[cell viewWithTag:NAME_TAG];
label.text = myObject.name;
and repeat this with other 2 labels. The tag is a unique number.
Put this code instead //setup your cell comment
I have created UIGridView. I believe your problem can be solved using the same technique.
You can learn the source code of UIGridView. The code is really short.
Bit late to the party, but we've open sourced our fully featured table component:
https://github.com/flexicious/iOSDataGrid
Some screenshots:
http://www.ioscomponents.com/Home/IOSDataGrid
Feel free to use!
Create a custom UIView subclass containing, say, three UILabel elements as subviews. Then set the cell's contentView property to this custom view.
add three labels as sub viewsinto uitableview cell's content.then assign apprrpriate values to it
eg:
- (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];
}
//frame should be set accordingly(means should be framed accordingly).
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100,100)];
UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(110,10,100,80)];
UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(115,60,100,50)];
[cell.contentView addSubview:l1];
[cell.contentView addSubview:l2];
[cell.contentView addSubview:l3];
return cell;
}