can i print the uitable customzie rows - tableview

my question if rows return 5 then UITableView show 0 to 5 index , but we want print the 3 to 5 any logic ??`
// Create the cell identifier
static NSString *CellIdentifier = #"CellIdentifier";
// Create the cell if cells are available with same cell identifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If there are no cells available, allocate a new one with Default style
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[no objectAtIndex:indexPath.row]valueForKey:#"name"];
// Configure cell...
can i print the value customize range

Your Solution is this and it is working perfectly:
in .h
Bool drawCell;
in .m
- (void)viewDidLoad
{
drawCell=NO;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if(indexPath.row==3)
{
drawCell=YES;
}
if(drawCell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.textLabel.text = [[no objectAtIndex:indexPath.row]valueForKey:#"name"];
}
else
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.frame=CGRectMake(0, 0, 0, 0);
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (drawCell) {
return 100.0f;
} else {
return 0.0f;
}
}

Related

Set value of variable after selecting row and add checkmark (xcode)

I'd like to do following: After clicking a row in a tableview, I want to set value of some variable and in the same time change the cell accessory type to checkmark. Here is my implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
tabledata = [[NSArray alloc] initWithObjects:#"row1", #"row2", #"row3", nil];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Row"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Row"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
return cell;
}
#pragma mark TableVoew Delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if row1 is selected change 'value' to 1 and change cell accessory to checkmark (other cells accessory to none)
//if row2 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
//if row3 is selected change 'value' to 2 and change cell accessory to checkmark (other cells accessory to none)
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.value = indexPath.row;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
EDIT
Set only the accessory for selected row in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.accessoryType = UITableViewAccessoryNone;
if (indexPath.row == self.value) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
See this to remove checkmarks from currently visible cells in tableview.
EDIT 2
To unselect the previous row, do this in didSelectRowAtIndexPath
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(#"Oldindexpath is: %#", oldIndexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
if (oldCell != newCell){
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}
This solved my problem with removing old checkmarks:
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
and problem with setting value of current row was solved in previous answer.

How to search NSMutableArray with UISearchBar?

I've searched many places but could not find the answer, so I decided to set my own question.
I populate a UITableView with data from JSON. My data structure is like this:
NSMutableArray* studentList = [(NSDictionary*)[responseString JSONValue] objectForKey:#"StudentList"];
JSON:
{
"StudentList":[
{
"StudentID":"08DH11039",
"StudentFirstName":"Nguyen Lam",
"StudentLastName":"Binhh"
},
{
"StudentID":"08DH11163",
"StudentFirstName":"Nguyen Minh",
"StudentLastName":"Duc"
},
{
"StudentID":"08DH11038",
"StudentFirstName":"Nguyen Bao",
"StudentLastName":"Khuyen"
},
{
"StudentID":"08DH11037",
"StudentFirstName":"Nguyen Tran Duy",
"StudentLastName":"Phuong"
}
]
}
Add data to UITableView:
- (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];
}
NSDictionary *student = [studentList objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%d. %# %#", indexPath.row+1, [student objectForKey:#"StudentFirstName"], [student objectForKey:#"StudentLastName"]];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I also add a UISearchBar but it didn't work:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[searchData removeAllObjects];
NSArray *student;
for(student in studentListCopy) //take the n group (eg. group1, group2, group3)
{
NSLog(#"%#", student);
NSMutableArray *newGroup = [[NSMutableArray alloc] init];
NSString *element;
for(element in student)
NSRange range = [element rangeOfString:searchString
options:NSCaseInsensitiveSearch];
if (range.length > 0) { //if the substring match
[newGroup addObject:element]; //add the element to group
}
}
if ([newGroup count] > 0) {
[searchData addObject:newGroup];
}
}
return YES;
}
Please help me to get it work. I want to search FirstName or LastName, which are displayed in TableView.
I'm new to iOS programming. Thank you very much.
I think this will be useful to you for understanding how the NSMutableArray is used with a UISearchBar and UITableView. The Example which I got from the internet is here. Maybe this will help you in solving your problem

How to center text in a UITableViewCell when accessory is UITableViewCellAccessoryCheckmark

I'm trying to keep text centred in a UITableViewCell when I have the accessoryView set to UITableViewCellAccessoryCheckmark. I'm also using a storyboard with this class.
Here's a screenshot of what I don't want.
How do I stop the text from being indented to the left?
My willDisplayCell method..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self cellToCheckmark]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(#"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Google";
break;
case 1:
cell.textLabel.text = #"Bing";
break;
case 2:
cell.textLabel.text = #"Yahoo!";
break;
default:
break;
}
}
if (indexPath.row == [self cellToCheckmark]) {
NSLog(#"Being Checked");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(#"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
return cell;
}
There are 2 things you need to do:
First, you need to make sure you set the table style to Default: UITableViewCellStyleDefault. All other styles use a detailTextLabel in one way or another and you won't be able to set the textLabel's alignment property.
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
Then you can set the alignment of your cell's textLabel:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
Then setting the accessory to checkmark based on whatever your data requires.
cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
Screenshot
iOS 8+ solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = ...;
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.layoutMargins = UIEdgeInsetsMake(0, 40, 0, 10);
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
}
return cell;
}
There seems to be a much simpler answer (See this answer). In short, you are centering the label in the cell's content view. Instead, center it in the cell itself, then it won't move.
Tried it out, it works on iOS 11.2.
These solutions work for default cells. If you have your own custom cell, the easiest way to do it is make the label have constraints both to the left margin and the right margin, then create an outlet for your left margin constraint, and set its constant in the cellForRowAtIndexPath method:
cell.accessoryType = isChosen ? .checkmark : .none
cell.leftMarginConstraint.constant = isChosen ? 40 : 0
This way, the right margin which is added for your accessoryType is then added to the left as well.
Don't know if the problem is solve
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.indentationLevel = 4;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.indentationLevel = 0;
}

Invalid index path for use with UITableView when selecting rows beyond a certain number

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return #"Substitutes";
}
- (int)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
int cnt=[subArray count];
return [subArray count];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *visible = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)){
NSUInteger row = [indexPath row];
NSString *subtitle =[[subArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *subDet =[[subDetArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
int height = [self heightOfCellWithTitle:subtitle andSubtitle:subDet];
return(height < CONST_Cell_height ? CONST_Cell_height : height);
}
return 40.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = #"SearchCell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)){
cell = [self CreateMultilinesCell:CellIdentifier];
cell.textLabel.text=[[subArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.detailTextLabel.text =[[subDetArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text=[[subArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.detailTextLabel.text =[[subDetArray objectAtIndex:row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndexPath == indexPath) {
selectedIndexPath = nil;
[table reloadData];
} else {
selectedIndexPath = indexPath;
//static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//cell.textLabel.text=#"test";
//cell.detailTextLabel.text =#"det Test";
[table reloadData];
}
[self.table deselectRowAtIndexPath : indexPath animated : NO];
[tableView beginUpdates];
[tableView endUpdates];
}
i implemented table view using above code.when i selecting row no. 26 v or more it
expands on selection but doesn't contract on next click.& gives me error on scroll
NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
code work smoothly for 25 rows. where i went wrong?
i got it finaly.:) selectedIndexpath gives garbage. i replace it by self.selectedIndexpath.
i didnt get why it do so on record no. 25 onwards.But this worked for me
Use like this
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-1 inSection:0];
CGRect rectOfCellInTableView = [objTableView rectForRowAtIndexPath:indexPath];
CGRect rectOfCellInSuperview = [objTableView convertRect:rectOfCellInTableView toView:[objTableView superview]];

all images in table view are the same in iPhone app

I have a table view where I want a different image for each cell based on logic. Here is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier] autorelease];
}
NSInteger *row = [indexPath row];
NSString *platform = [[myStringArray objectAtIndex: row];
cell.imageView.image = [self imageForInventory:platform];
return cell;
}
Here is my imageForInventory method:
- (UIImage *)imageForInventory:(NSString *)platform {
if (platform = #"Win") {
UIImage *winImage = [UIImage imageNamed:#"Vista.png"];
return winImage;
}else if (platform = #"Mac") {
UIImage *appleImage = [UIImage imageNamed:#"Apple.png"];
return appleImage;
}else if (platform = #"Linux") {
UIImage *linuxImage = [UIImage imageNamed:#"Linux.png"];
return linuxImage;
}
return nil;
}
This shows the winImage for every row ignoring the if statements. How do I get the image to set accordingly per cell?
It's probably your comparison code, to compare strings in imageForInventory use [str1 isEqualToString:str2]. You're using = which is the assignment operator right now, but even if you change it to == it wouldn't work.

Resources