When I do simple HelloWorld app I have problem with
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]
,this method doesn't work... and i don't know why? pliz help
Code:
- (void)viewDidLoad
{
int i = 0;
[super viewDidLoad];
while (i < 10 ) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
i++;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = #"HelloWorld";
return cell;
}
App should create 10 cells with Label #"HelloWorld"
You're going about this the wrong way. If you want 10 cells you shouldn't be trying to add them one at a time, you should just return 10 in numberOfRowsInSection, e.x:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
Additionally, nothing will show up in the cells because you have call alloc/init on the cells in cellForRowAtIndexPath. Modify your code to do so like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"HelloWorld";
return cell;
}
Related
I tried to hide my detailTextLabel.cell while my tableview loads,by the code,
cell.textLabel.text=[array objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden = YES;
While selecting the row, try to display the detailarray in didSelectRowAtIndexPath, as expected the detailarray is displaying while am pressing the row and once i stop pressing it, the detailarray text disappears, why it is happening so,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
[self.tableview beginUpdates];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden = NO;
[self.tableview endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex)
{
return 77;
}
return 44;
}
EDITED:
Assigned a variable 'a' in .h file and used the code as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
NSLog(#"dealarray %#\n %#",dealarray,detailarray);
if (a==-1) {
cell.detailTextLabel.hidden = YES;
}
else if(a==indexPath.row)
{
cell.detailTextLabel.hidden = NO;
}
else cell.detailTextLabel.hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
a=indexPath.row;
[tableview reloadData];
}
For this, You can use two type of cell.
For normal data, use basic cell. On selection, reload table or cell and use another detailLabel cell for selected row only.
And it will solve your issue.
Thanks
If i understood your requirement, Can you try below code to resolve your purpose.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
/* .
Write necessary code here....
*/
// -------------
cell.textLabel.text=[array objectAtIndex:indexPath.row];
if (![selectedRowIndexs containsObject:[NSNumber numberWithInt:indexPath.row]])
{
cell.detailTextLabel.hidden = YES;
}
else
{
cell.detailTextLabel.hidden = NO;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden = NO;
if (![selectedRowIndexs containsObject:[NSNumber numberWithInt:indexPath.row]])
{
[selectedRowIndexs addObject:[NSNumber numberWithInt:indexPath.row]];
}
}
I have made customCell for my UITableView. I am downloading data from server and showing on CustomCells.
The method of manipulating data is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCellReuseID";
LeaderBoardCell *cell = [leaderBoardTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[LeaderBoardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
GetScoreBoard *ob = [leaderBoardArray objectAtIndex:indexPath.row];
[[cell title] setText:ob.fullname];
[[cell discription] setText:ob.points];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [leaderBoardArray count];
}
The CustomCell "LeaderBoardCell" is loading later, but before this I see simple cells which are default in UITableView.
Is is possible to load only CustomCell in TableVeiw?
I'm unable to populate the second UITableView Controller, wondering if anyone could help.
I'm using a websites API, JSON, and RestKit for the data. I believe that part is working fine because my first VC loads fine.
But I'm not sure if I need to use prepareForSegue and/or didSelectRowAtIndexPath so that I can identify the cell/row selected in the first VC, so that the second VC populates with the (correct) data.
1st VC populates correct:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sports.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.leagues.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Sport *sport = [sports objectAtIndex:section];
return sport.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Sport *sport = [sports objectAtIndex:indexPath.section];
League *league = [sport.leagues objectAtIndex:indexPath.row];
cell.textLabel.text = league.name;
return cell;
}
2nd VC populates blank table:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return leagues.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.teams.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
League *league = [leagues objectAtIndex:section];
return league.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
League *league = [leagues objectAtIndex:indexPath.section];
Team *team = [league.teams objectAtIndex:indexPath.row];
cell.textLabel.text = team.name;
return cell;
}
Or if I try to add extra code for 1st VC, app crashes before getting to 2nd VC:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TeamsViewController *teamsViewController = [[TeamsViewController alloc] initWithNibName:#"TeamsViewController" bundle:nil];
teamsViewController.title = [[sports objectAtIndex:indexPath.row] objectForKey:#"sports"];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"leagueDetail"]) {
TeamsViewController *tvc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
tvc.data = [self.navigationController objectInListAtIndex:indexPath.row]
}
Would really appreciate any help!
I am a little bit confused with excepting of creation UITableViewCell object. When you ask table view for dequeuing cell it returns one which it does not need at the moment, if there are no unused cell you have to create a new one. Try code like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create cell
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [NSString stringWithFormat:#"cell %d", indexPath.row];
return cell;
}
I want to design a dynamic table which has 2 sections, first section has 4 cells and second one has 41 cells. I want to program these 2 sections separately but I can only modify one section. how can I modify each section separately. The codes that I wrote so far is like this:
{
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 4;
else
return 41;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Questions";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Questions"];
cell.textLabel.text = [NSString stringWithFormat:#"Question %lu", (unsigned long)indexPath.row +1];
return cell;
}
}
The NSIndexPath has both a row property and a section property. From these two properties, you can tell which cell you are currently working on in
<(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
For example:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Questions";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Questions"];
if (indexPath.section == 0)
{
cell.textLabel.text = [NSString stringWithFormat:#"1st Group - Question %d", indexPath.row + 1];
}
else
{
cell.textLabel.text = [NSString stringWithFormat:#"2nd Group - Question %d", indexPath.row + 1];
}
return cell;
}
If I call reloadRowsAtIndexPaths for the first cell of a section, with previous section empty and the one above not-empty, I get a strange animation glitch (even if I specify "UITableViewRowAnimationNone") where the reloaded cell slides down from the above section..
I tried to simplify the example as much as possible:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 1;
else if (section == 1)
return 0;
else if (section == 2)
return 3;
return 0;
}
- (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] autorelease];
}
// Configure the cell...
cell.textLabel.text = #"Text";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *editedCell = [[NSArray alloc] initWithObjects:indexPath, nil];
//[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:editedCell withRowAnimation:UITableViewRowAnimationNone];
//[self.tableView endUpdates];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Section";
}
Actually you can comment out the last method, but it gives a better understanding of the problem.
You can set values you want to the cell directly, not letting table to reload itself (and thus avoiding any unwanted animations). Also to make code clearer and avoid code duplication lets move cell setup to a separate method (so we'll be able to call it from different locations):
- (void) setupCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
cell.textLabel.text = #"Text"; // Or any value depending on index path
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self setupCell:cell forIndexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// create cell
// Configure the cell...
[self setupCell:cell forIndexPath:indexPath];
return cell;
}