I have a UITableViewController with an attached xib file. Inside the xib file i have a TableViewCell object with a view inside it along with labels on the view. Below is an image of what my result should be:
pic1 http://casperslynge.dk/2.png
And below is how my tableview is looking atm.
pic2 http://casperslynge.dk/iphone1.png
My issue is that I can't get the table view cell to fill the entire window. Have tried everything inside the interface builder. Nothing to do with the origin or the width/height. Below is how I instantiate my cell with the xib file and fill my labels with data.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CompanyListCellIdentifier =
#"CompanyList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CompanyListCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CompanySubscriptionView" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
NSUInteger row = [indexPath row];
CompanyModel *company = [list objectAtIndex:row];
name.text = company.name;
network.text = company.networks;
NSString* subscriptionCountString = [NSString stringWithFormat:#"%d", company.subscriptions.count];
subscriptionCount.text = subscriptionCountString;
fromPrice.text = company.fromPrice;
NSString* trustScoreString = [NSString stringWithFormat:#"%d", company.trustScore];
trustScore.text = trustScoreString;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
The weird thing is that in the next view when one of the cell's are pressed, I have no issue making the view fill the entire width and height, and it is created in exactly the same way (see picture below). Is it something in the xib file that I am doing wrong or in the code?
pic3 http://casperslynge.dk/iphone2.png
Can anybody help?
As I see from screenshots you are trying to use table view with grouped style while you definitely need a plain one (at least for first view).
Related
to the point, i have custom cells, inside it has 2 label and 1 textfield. both label and textfield got input from user. i also have other view that has uitableview inside it. my question is how do i populate cell in uitableview? please help.
this is my code inside tableviewcontroller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1; // i want to populate this using 'count' but i dont know how.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
cell.titleLabel.text = [NSString stringWithFormat:#"%#",titleTextString];
cell.timerLabel.text = [NSString stringWithFormat:#"%#",timerString];
cell.statusLabel.text = [NSString stringWithFormat:#"%#",statusString];
return cell;
}
how do i populate my tableview if i push add button after finishing input by user? Please if you dont mind help me with code. i'm beginner and im hard to understand by using opinion.
If I understood your question correctly, you did a custom nib file for your cells that has 2 UILabel in it and one UITextField, and you want to access these objects when populating your table. Here are some steps for this issue:
First, you have to give a tag number for each object in your custom cell. You find this property in the Attribute Inspector in Interface Builder. Say you gave the first label tag 1, the second label 2 and the text field 3.
Second you have to give a. Identifier for this nib file, for example MyCustomCellIdentifier. This identifier will be used later on in the view that has the table so you can link to it.
Third, also in the custom cell nib, you click on the yellow square that says File's Owner and in the Identity Inspector you change the Class to the class name that has the table that will use this custom cell.
Fourth, in the class that you have the table that will use the custom cell, create an outlet of type UITableViewCell. We will link this in the custom nib cell.
Fifth, goto the custom nib cell, click on the cell window, then in the Connections Inspector link New Referencing Outlet to the File's Owner, you will see the outlet that you created in the table class showing here, simply link to it.
Now since the connections are established thing are more easy, in the cellForRowAtIndexPath function (in the class that contains the table for sure), you have to load the custom cell from the nib file as follows:
static NSString *tableIdentifier = #"MyCustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TheNibClassNameOfYourCustomCell" owner:self options:nil];
if([nib count] > 0) cell = theNameOfTheOutletYouUsed;
else NSLog(#"Failed to load from nib file.");
}
Ok, your custom cell is loaded in variable cell, now you have to access every object in it from the tags you created:
UILabel *label1 = (UILabel *)[cell viewWithTag:1];
UILabel *label2 = (UILabel *)[cell viewWithTag:2];
UITextField *textField1 = (UITextField *)[cell viewWithTag:3];
Now you can access everything through label1, label2, and textField1 easily like label1.text = #"Hi";
I hope this answers your question.
I have configured a table view cell, whose tables cell is loaded from another xib, both the filename and the identities are called,BlogViewControllerTableCell
Most of them works properly, but some contents in the cells keep changing while scrolling.
No matter I use the array value, or simply put the [indexpath row].
The below is the code,
I would be appreciate for your help.
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BlogViewControllerTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"BlogViewControllerTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
student_name.text = [NSString stringWithFormat:#"%#", [[student_list objectAtIndex:[indexPath row]] objectForKey:#"name"]];
return cell;
}
I am having trouble wiht UITableViewCell loaded from nib. I used apple documentation to that but I do't know where I am going wrong. UITableViewCell created through IB contains 5 UIlable. Nib are loaded perfectly but the problem is when I scroll text of the label changes automatically to different.
Following is the my code for cell for row at indexpath. Please let me know where I am going worng.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowNo = [indexPath row];
NSLog(#"Row No:%d",rowNo);
Transfer *tempTransferRecord =(Transfer*)[self.transferInformation objectAtIndex:rowNo];
seasonYear.text=tempTransferRecord.seasonYear;
longName.text=tempTransferRecord.longName;
transactionDate.text=tempTransferRecord.transactionDate;
toTeam.text=tempTransferRecord.toTeam;
fromTeam.text=tempTransferRecord.fromTeam;
/*
static NSString *MyIdentifier = #"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier] ;
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"PlayerTransfers" owner:self options:nil];
cell=transfersInfoCell;
self.transfersInfoCell=nil;
}
*/
static NSString *CellIdentifier = #"MyIdentifier2";
static NSString *CellNib = #"PlayerTransfers";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
// perform additional custom work...
return cell;
}
Try moving your cell loading code to the top of cellForRowAtIndexPath, and the rowNo and value setting after that. Cells changing content must mean you're either changing what's in the data source between when you first set it and when you scroll, or you're setting values at the wrong point in time (i.e setting values on row 5 but then loading row 6 from a nib, or setting values on a row but then loading the nib for it afterwards so the values you set are overridden). Also, I'm a bit concerned that you set local variables rather than, say, (PlayerTransfers*)cell.seasonYear.text = tempTransferRecord.seasonYear or (UILabel *)[cell viewWithTag:1].text = tempTransferRecord.seasonYear. Shouldn't you be setting the values within each cell rather than on your table view class? That could explain why values are changing to something different: you may use the same variables to set every cell.
Have you set a CellIdentifier in Interface Builder?
I have a custom UITableViewCell loaded from a nib file. Everything works fine until I scroll down past the last cell such that the table view has to bounce back when I let go.
Then, the cells towards the top of the list are blank and remain blank until I manually refresh the table view.
Image of issue: [iPhone Screenshot][1]
Here is my cell for row code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TimeEntryCellIdentifier";
TimeEntryCell *cell = (TimeEntryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TimeEntryCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[TimeEntryCell class]]) {
cell = (TimeEntryCell *) oneObject;
}
}
}
TimeEntry *aTimeEntry = [appDelegate.timeEntries objectAtIndex:indexPath.row];
cell.clientName.text = aTimeEntry.ClientName;
cell.category.text = aTimeEntry.CategoryName;
cell.hours.text = [NSString stringWithFormat:#"%#", aTimeEntry.Hours];
[TimeEntry release];
return cell;
}
Any ideas?
[1]: http://dl.nvthost.com.s3.amazonaws.com/Screen shot 2011-02-18 at 2.46.42 PM.png
[1]: http://dl.nvthost.com.s3.amazonaws.com/Screen shot 2011-02-18 at 2.46.42 PM.png
I don't think you should be doing the
[TimeEntry release];
there on the next to last line. You didn't allocate it in that method, just pulled a reference from the delegate. The retain count is probably zero at that point. When iOS starts releasing memory that TimeEntry will be dealloc'd.
The problem turned out the be the view hierarchy in IB. Even though I put a view in the cell and then dragged UILabels onto the view, they ended up on the same level as the view. After I moved the UILabels under the view it appears to be working.
What I've done is created a custom TableCell view that gets populated with information from an array of objects. Each TableCell gets loaded in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSUInteger row = [indexPath row];
NSDictionary *rowData = (NSDictionary *)[self.surveys objectAtIndex:row];
cell.info1.text = [rowData objectForKey:#"info1"];
cell.info2.text = [rowData objectForKey:#"info2"];
cell.info3.text = [rowData objectForKey:#"info3"];
cell.otherInfo = [rowData objectForKey:#"otherInfo"];
return cell;
}
In addition to this I specify a custom height for the cell here
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 76.0;
}
When the value for tableView: heightForRowAtIndexPath: is 76 it loads all the cells that I can see and they aren't ever blank. Without changing anything else in the code I can modify the value in tableView: heightForRowAtIndexPath: and it will only show the first cell value when the view is loaded. As I scroll down they are refreshed with a value as soon as their top edge hits the top of the screen. When I get back up to the top and stretch the view so that it bounces back the bottom ones will disappear.
As far as I can tell, the change in height somehow affects how they are loaded but I can't for the life of me see how.
I specified a height in the custom cell .xib that was different than the one I was specifying in code. Once this was changed to match the size specified in the code the problem went away.
Another thing to pay attention to isn't just the size of the Custom table view cell's height but also the height of other sub views as well.
I had this problem when one of my UIImageView objects was higher than the cell height it would cause this very same problem.
Thanks for the post as it helped me sort out my issue too. +1 (wont let me vote up or I would, sorry)