How to get all rows of same group in TableView - iphone

Please help me to get all rows of same group in tableView. i am using below code to show radio/check in groups of tableview and want the only one per group selection.
so for that i have got 2 images
1:uncheck
2:checked
so when user will click/select a row of group than i am changing its cell.image to "checked" and its working but don't know how to make all other cell's images of same group's to "unchecked"
dont know how to loop through (get) all rows/cells of current group
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.image = [UIImage imageNamed:#"checkbox-checked.png"];
NSString *message = [[NSString alloc] initWithFormat:rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Here is a possible workaround:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Store the section in your class
NSUInteger section = indexPath.section;
self.section = section; // remember to declare instance variable, property, synthesize
self.row = row; // remember to declare instance variable, property, synthesize
[tableView reloadData];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == self.section) {
if (indexPath.row != self.row) {
// then do UNCHECK here
}
}
}
If you know how many rows in your current section, you don't need to call reloadData, just call [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION];

Related

How to set checkbox image while selecting table view section?

I'm working on a project and i stuck at a point. My project has a Table View with some data after clicking on the cell table view will expended into sections. Every thing is fine except one thing i.e i want to set a checkbox image (when the user clicked on a cell).
when i tap on the cell it gives the cell title text but i am not able to set the checkbox image (like UITableViewCellAccessoryCheckmark)on particular cell section.
I am posting the images and the codes below: !
MyTableView it contains the Root Exercise Names
This is the Expended View and i want a checkbox(UITableViewCellAccessoryCheckmark) at the right side of the cell.
When i click on tableView didSelectRowAtIndexPath: it works fine
//code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"]; //from plist
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark- Table View DataSource and Delegates
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
My Question is How to show Checkbox at the selected section?
Sorry! if it is not formatted properly(New to stack)!
Thank You!
I was finally able to get it working. Here's what you have to change:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
_cell.accessoryView = nil;
if ([self.selectedCells containsObject:indexPath])
{
_cell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
And
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"];
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (!thisCell.accessoryView)
{
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
// [alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
If nothing else has changed in those methods, just copy-paste the code and it will work. I tested it before posting it. Let me know if you have more questions
UPDATE
Here is the explanation of what was going on. There were actually a couple of problems:
First, you were mixing accessoryType and accessoryView; this was causing an
incorrect execution of the if-else clause. Here is the old code
// here you are using accessoryType
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
// but here you're using accessoryView
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// here too...
thisCell.accessoryView=nil;
thisCell.accessoryView = nil;
}
Second, you were not saving the indexPaths of the selected elements. Here is the corrected code (note is the same if-else clause):
if (!thisCell.accessoryView)
{
// you have to save the selected indexPaths
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// if the cell was already selected, then remove the indexPath
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
Third, the following line inside of cellForRowAtIndexPath... was messing up things because it was showing another checkmark, so I just removed it:
[_cell.checkUnCheck_ImageView setHidden:NO];
That was pretty much it.
Hope this helps!
You have the right idea of how to do this, however, you're setting the checkmark in the wrong place. You should use the method -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to store (remember) which cell was selected and NOT to set the accessory type.
If only one cell can be selected at a time you can declare aNSIndexPath property to remember the cell that was selected. However, if multiple cells can be selected at a time you can declare a NSMutableArray where you can store all the cells that have been selected.
Then you should use the method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to set the checkmark to the cells that were selected.
Here is an example (I'm assuming multiple cells can be selected):
In your *.h file declare a NSMutableArray property
#property (nonatomic, strong) NSMutableArray *selectedCells;
In your *.m file, inside your init method initialize the property:
_selectedCells = [[NSMutableArray alloc] init];
Then, in the didSelectRowAtIndexPath... method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedCells containsObject:indexPath]) {
[self.selectedCells removeObject:indexPath]; // this line allows the checkmark to be shown/hidden if the user presses several times the same cell
} else {
[self.selectedCells addObject:indexPath]; // a cell was selected; remember it
}
... // the rest of your code
}
Finally, in the cellForRowAtIndexPath... method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... // Create, initialize and customize your cell
// if the cell you're being asked for is saved in the array it means it was selected
if ([self.selectedCells containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
And that's it. Let me know if you have more questions
Hope it helps!
UPDATE
If all you want to do is to set a custom image as your checkmark just create the image and assign it to the accessory view while using the code that was already working for you, like so:
// Note that you should use 'accessoryView' and NOT 'accessoryType'
if (thisCell.accessoryView == UITableViewCellAccessoryNone)
{
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
}
else
{
thisCell.accessoryView = nil;
}

How to perform Validation in ios uitableview (group table)?

In my app i have used UItableview group table.
In which there are two section and i want to perform validation on it.
for eg:In section 1 there are two row's,if user select row one user have to select some value from section 2 and if user have select second row in 1 section then no need of selection in section two.
following is my code:
- (void)viewDidLoad
{
NSArray *arr_data1 =[[NSArray alloc]initWithObjects:#"Yes",#"No",nil];
NSArray *arr_data2 =[[NSArray alloc] initWithObjects:#"Monotherapy",#"Adjunctive",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arr_data1,#"",arr_data2,
#"Was it monotherapy or adjunctive",nil];
self.tableContents =temp;
self.arr_data =[[self.tableContents allKeys]
sortedArrayUsingSelector:#selector(compare:)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arr_data count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.arr_data objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSLog(#"fdfdfdf=%d",[indexPath row]);
NSString *rowValue = [listData objectAtIndex:row];
if ([rowValue isEqualToString:#"Yes"])
{
NSString *message = [[NSString alloc] initWithFormat:#"%#",rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
//do as per you requirement
}
else if(indexPath.section == 1)
{
//do as per you requirement
}
}
You can first declare a variable count to keep track of how many items are selected in section 2 and isItemRequired as a flag variable and sythesize them, then use these delegates:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
A sample code to begin with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
isItemRequired = YES;
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"You must have to select one or more item in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
else
{
isItemRequired = NO;
}
}
else if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"Atleast one item should be selected in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
}
In the didselect method of UITableView, you needs to check the section number & then check which row is clicked from that section
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
//do this
}
else if (indexPath.row == 1)
{
//do this
}
}
else
{
//do this
}
}
In the following delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
add a check with the help of indexPath returned by the delegate, it will tell you that which section's which row is selected like :
if(indexPath.section == 0 && indexPath.row == 0)
{
// Select something from the another section also
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
// No need to select from another section.
}
First Take one BOOL variable with isYes in .h file like bellow...
BOOL isYes;
after in viewDidLoad: method just assign it to NO like bellow..
isYes = NO;
after that in didSelectRowAtIndexPath delegate method just use like bellow...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0)
{
isYes = YES;
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
isYes = NO;
}
if(isYes){
if(indexPath.section == 1 )
{
//here user can select the section 2
}
}
}
i hope this help you....

How to get the alert message?

I have this code and actually i want an alert message when user Tap the delete message ?? how can i do that ..
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *DeleteMeCellIdentifier = #"DeleteMeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DeleteMeCellIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Please try this code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:[NSString
stringWithFormat:#"deleted row no. %#",indexPath.row] delegate:nil
cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
You can try this:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:[NSString stringWithFormat:#"You have deleted row no. %#",indexPath.row] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}

how to implement code for saving multiple array items through the alertview?

I have implemented code for alert view with textfield here when i click on ok button the textfield entered value is storing in string formate as (str) and i am adding that value to the array in order to i am loading array to the table view but here problem is every time table view consists one item but i want store multiple items entering items from alert and save it as array item for this how to implement code for saving multiple array items through the alertview any one help me to solve this problem in iphone.
myAlert = [[UIAlertView alloc] initWithTitle:#"Enter year"
message:#"alert message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:#"" label:#"entervalue"];
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
str = alertTextField.text;
[savedarray addObject:str];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"]) {
str = alertTextField.text;
[savedarray addObject:str];
[self.yourtableview reloadData]; // reload your tableview when add new data in array.
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return savedarray.count;
}
- (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 = [savedarray objectAtIndex:indexPath.row];
return cell;
}
Hope, this will help you..
If i am rit u r asking for something like this:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Ok"])
{
str = alertTextField.text;
if([savedarray Count] < yourRequiredItemsCount)
{
[savedarray addObject:str];
alertTextField = #"";
[alertView show];
}
else
{
[alertView release]; //Release the Alert here.
}
}
}
tell me if it helps! will give u an alternate sol.
If you're allocating the savedarray correctly, then the posted code looks reasonable. The next thing to check is whether you are properly using savedarray as the datasource for the table.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return savedarray.count;
}
- (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 = [savedarray objectAtIndex:indexPath.row];
return cell;
}
Be sure to set the same view controller as the tableview's delegate and datasource.

I can't fill the cells of my UITableView with a NSArray (Normally it always works, but not in the case)

I have a UITableView with Cells that are filled with the Objects of an NSArray. I´m using the MGTwitterEngine to get Tweets from users. I want to show this tweets in the TableView. Here is the method that is called when the MGTwitterEngine has received all tweets ("namen" and "nachricht" are NSArrays):
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{ NSLog(#"Got statuses for %#:\r%#", connectionIdentifier, statuses);
NSDictionary *userDict = [statuses valueForKey:#"user"];
namen = [[NSArray alloc] initWithObjects: [userDict valueForKey:#"name"], nil];
nachricht = [statuses valueForKey:#"text"];
NSLog(#"%#", namen);
NSLog(#"%#", nachricht);
[table reloadData];
}
Then I did this to fill the cells with the names of the Twitter-users:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [nachricht count];
}
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
return cell;
}
Unfortunately, the app crashes whenever the table is reloaded: [table reloadData];. I know that the NSArray got all the right values, because NSLog shows me that. I've tried also to create a other NSArray, like that:
arryData = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
And I written this into the cellforRowAtIndexPath-methode and and everything worked well. Why does it not work with the "namen"-NSArray?
The NSArray is probably being released - have you examined what's at its memory address?