iPhone Coding: Problem with UITableView selection - iphone

I have a strange problem with my UITableView. I want all the cells in the table to be selectable, such that if a cell is selected, an appropriate action is executed. All the cells in my table are currently selectable apart from the cell at row 0 (the cell at that appears at the top of the table). This cell is not selectable, even though it has been set to allow for selection. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableView.allowsSelection = YES;
static NSString *SettingsTableID = #"SettingsTableID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingsTableID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier: SettingsTableID] autorelease];
}
cell.textLabel.text = [tableHeadingsArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Many thanks.

Sorry everyone, I'm being stupid. :) This code was in my ViewController sub-class. I copied the class from an example, and forgot to check it over thoroughly before using it.
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
Deleting this has fixed the problem.
Cheers.

Related

set text for cells in uitable different

I have a table with a few rows, and the place holder text for all is the same. How can I programmatically change the placeholder text of only the first one?
Thanks!
You need to run an if statement to check the index of the cell. 0 is the first index, so you would need to check to see if the row is 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];
}
if (indexPath.row == 0)
cell.textLabel.text = #"First Cell";
else
cell.textLabel.text = #"Not First Cell";
}
In order to access your cell AFTER the cell was created/reused, try the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method of your uitableview object! See the following Apple doc for a detailed elaboration on this method.
Note:
Return Value
An object representing a cell of the table or nil if the cell is not
visible or indexPath is out of range.

UITableView moves cell contents around when scrolling

I have seen other questions on here related to this, but I have their solutions in place, and things still move around when I scroll.
Basically, I have a table of data and I want the first and only the first row to have a UITableViewCellAccessoryDisclosureIndicator. The problem is, when I scroll around, the indicator duplicates, deletes, and moves to other cells. Here is my code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
return cell;
}
The problem is something with the indexPath.row variable.. its somehow returning 0 in other cells that aren't the very first (when it comes to displaying cells). HOWEVER... the data is always right from my array(which implies the value HAS to be correct) and in my
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
method, i only wish to change screens when row 0 is clicked.. and regardless of where I have scrolled to, no cells trigger except the very first. So it seems THAT check is always correct while the one inside cellForRowAtIndexPath is not...
Most likely, you are re-using cells that already have the accessoryType set. You need to explicitly set it to NOT show on cells where it shouldn't.
if(indexPath.row == 0){
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}

cellForRowAtIndexPath always return nil

I have created sample application that insert 12 rows in tableview.And inserted fine.When after inserted my rows i dont want any changes in text during scrolling of tableview.So i checked with indexpath row has value of UITableViewCell , if it has values means return that cell otherwise we created new UITableViewCell.
My sample code is below
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"GlobalCount = %d",GlobalCount);
return GlobalCount;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *obj = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if(obj!=nil)
{
NSLog(#"cell indexpath row = %d",indexPath.row);
NSLog(#"cell text = %d",obj.textLabel.text);
return obj;
}
else {
NSLog(#"obj==nil");
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if([DataTable count]>0)
cell.textLabel.text = [DataTable objectAtIndex:0];
// Configure the cell.
return cell;
}
What my objective is , i dont want update any text (or UITableViewCell)for already created indexpath rows(of cell).So i can check this in cellForRowAtIndexPath , but it always return nil.If i am missing anything ?
I checked using this line
UITableViewCell *obj = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
if(obj!=nil)
Plz help me?
Thanks in advance......
Let the table view decide id it need to update the cell or not. Just remove all the firsdt part of your code and everything will run smoothly.
Don't hesitate to read the UITableView doc.

UITableView Checklist Problem

I have this bizarre problem. I'm making a checklist program with XCode and I'm using UITableView with UITableViewCellAccessoryCheckmark. I can select cells and the checkmark will appear, but somehow, other cells that I have NOT yet selected below will also have a checkmark appear. Any ideas?
Here's my check mark coding:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
cell = [aTableView cellForRowAtIndexPath: indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
I don't know if this affects it, but I also implemented this method:
- (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;
}
// Configure the cell.
if(tableView == Table1){
switch(indexPath.section){
case 0:
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
break;
}
//cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
}
if(tableView == Table2){
cell.textLabel.text = [array4 objectAtIndex:indexPath.row];
}
return cell;
}
Thanks.
Set the cell.accessoryType each time you call tableView:cellForRowAtIndexPath:. Otherwise, when you reuse a cell, you'll get it's accessoryView instead of what you're expecting.
So, yeah, you'll need to keep track in when NSIndexPaths are selected by some method other than just looking at the accessoryType.
You should keep the checked/unchecked info in data source (array).
I also advise you to remove the cell.accessoryType = UITableViewCellAccessoryNone; line.
This line will be executed only for few first cells. All the other cells will be "reused" - meaning that the old (already initiated) cells will be used, and you will have to modify the details that are displayed on these cells (all inside cellForRowAtIndexPath as you do now).
In addition you will have to add a similar line (something like cell.accessoryType = ([[array objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;).
In general, I suggest you to use one array for holding the data for your table, when each item in the array will be a dictionary. This way you will be able to hold the texts and the boolean checkmarks (inside NSNumber) and easily access them when needed.

iphone - when exactly does cellForRowAtIndexPath for table views get called and by what in

I just have a basic (noob) question about cellForRowAtIndexPath get called?
I'm working through example code and I don't see it explicitly called anywhere?
Does any component of type UITableViewComponent automatically call this function when it is created?
Thanks
When a UITableView is displaying this method gets called per row. In it you will customize each cell with particular data for display.
Here's the class reference.
Here's a sample:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FriendCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
Item *i = [itemArray objectAtIndex:row];
cell.textLabel.text = [i name];
cell.detailTextLabel.text = [i brewery];
[i release];
return cell;
}
This is called for each cell when the corresponding UITableView gets drawn. An index path is passed in, and based on the section and cell numbers, your code should generate a UITableViewCell to be displayed in the UI.
You can look here for a quick tutorial into how UITableViews work.